无法通过业力测试传递给控制器
Cannot get karma test to pass on controller
我正在尝试在 angular js 中创建一个 "To Do list"。完成任务后,我希望能够单击复选框以标记完成。虽然它正在 index.html 上工作,但我无法让测试本身通过。
这是我的测试:
describe('ToDoListController', function() {
beforeEach(module('ToDoList'));
var ctrl;
beforeEach(inject(function($controller) {
ctrl = $controller('ToDoListController');
}));
it('initialises with an empty list and term', function() {
expect(ctrl.listDisplay).toEqual([]);
expect(ctrl.taskTerm).toBeUndefined();
});
describe('when viewing the to do list', function(){
it('displays list items', function() {
ctrl.taskTerm = "hello";
ctrl.addTask();
expect(ctrl.listDisplay[0].task).toBe("hello");
});
it('is completed when clicked', function() {
ctrl.taskTerm = "hello";
ctrl.addTask();
ctrl.isCompleted("hello");
expect(ctrl.listDisplay[0].completed).toBe(true)
});
});
});
这是我的控制器:
toDoList.controller('ToDoListController', [function() {
var self = this;
self.listDisplay = []
self.addTask = function() {
self.listDisplay.push({task: self.taskTerm, completed: false})
};
self.isCompleted = function(item) {
var i = self.listDisplay.indexOf(item)
self.listDisplay[i].completed = true
}
}]);
我返回的错误是:
TypeError: Cannot set property 'completed' of undefined
at self.isCompleted (/Users/edobrien/Documents/Projects/todo_challenge/js/toDoListController.js:13:35)
at Object.<anonymous> (/Users/edobrien/Documents/Projects/todo_challenge/test/toDoListController.spec.js:26:12)
供参考,错误发生在 "ctrl.isCompleted("hello");"行
非常感谢你们能提供的任何帮助!
你的控制器的isCompleted
方法是错误的:你的列表是一个对象数组,没有字符串:
self.addTask = function() {
self.listDisplay.push({task: self.taskTerm, completed: false})
};
如果你设置taskTerm为"hello",压入数组的对象不是字符串"hello",而是像
这样的对象
{task: "hello", completed: false}
所以当你试图用indexOf("hello")找到它时,它总是return -1,因为
"hello" != {task: "hello", completed: false}
更改在数组中查找对象的方式:
self.isCompleted = function(item) {
var completed=false;
self.listDisplay.forEach(function (value,index) {
if (value.task ==item) {
completed=value.completed;
}
});
return completed;
};
UPDATE:我在阅读您的代码时犯了一个错误:isCompleted
是 checks 方法的常用名称如果某事……好吧,如果某事完成了。但实际上您的方法 设置 它已完成。这类方法通常命名为setCompleted
.
所以方法应该是这样的:
self.setCompleted = function(item) {
self.listDisplay.forEach(function (obj,index) {
if (obj.task ==item) {
obj.completed=true; //setting it as completed
}
});
};
我正在尝试在 angular js 中创建一个 "To Do list"。完成任务后,我希望能够单击复选框以标记完成。虽然它正在 index.html 上工作,但我无法让测试本身通过。
这是我的测试:
describe('ToDoListController', function() {
beforeEach(module('ToDoList'));
var ctrl;
beforeEach(inject(function($controller) {
ctrl = $controller('ToDoListController');
}));
it('initialises with an empty list and term', function() {
expect(ctrl.listDisplay).toEqual([]);
expect(ctrl.taskTerm).toBeUndefined();
});
describe('when viewing the to do list', function(){
it('displays list items', function() {
ctrl.taskTerm = "hello";
ctrl.addTask();
expect(ctrl.listDisplay[0].task).toBe("hello");
});
it('is completed when clicked', function() {
ctrl.taskTerm = "hello";
ctrl.addTask();
ctrl.isCompleted("hello");
expect(ctrl.listDisplay[0].completed).toBe(true)
});
});
});
这是我的控制器:
toDoList.controller('ToDoListController', [function() {
var self = this;
self.listDisplay = []
self.addTask = function() {
self.listDisplay.push({task: self.taskTerm, completed: false})
};
self.isCompleted = function(item) {
var i = self.listDisplay.indexOf(item)
self.listDisplay[i].completed = true
}
}]);
我返回的错误是:
TypeError: Cannot set property 'completed' of undefined
at self.isCompleted (/Users/edobrien/Documents/Projects/todo_challenge/js/toDoListController.js:13:35)
at Object.<anonymous> (/Users/edobrien/Documents/Projects/todo_challenge/test/toDoListController.spec.js:26:12)
供参考,错误发生在 "ctrl.isCompleted("hello");"行
非常感谢你们能提供的任何帮助!
你的控制器的isCompleted
方法是错误的:你的列表是一个对象数组,没有字符串:
self.addTask = function() {
self.listDisplay.push({task: self.taskTerm, completed: false})
};
如果你设置taskTerm为"hello",压入数组的对象不是字符串"hello",而是像
这样的对象{task: "hello", completed: false}
所以当你试图用indexOf("hello")找到它时,它总是return -1,因为
"hello" != {task: "hello", completed: false}
更改在数组中查找对象的方式:
self.isCompleted = function(item) {
var completed=false;
self.listDisplay.forEach(function (value,index) {
if (value.task ==item) {
completed=value.completed;
}
});
return completed;
};
UPDATE:我在阅读您的代码时犯了一个错误:isCompleted
是 checks 方法的常用名称如果某事……好吧,如果某事完成了。但实际上您的方法 设置 它已完成。这类方法通常命名为setCompleted
.
所以方法应该是这样的:
self.setCompleted = function(item) {
self.listDisplay.forEach(function (obj,index) {
if (obj.task ==item) {
obj.completed=true; //setting it as completed
}
});
};