更新
我已经更新了我的测试和错误信息回来是非常混乱的..。
it('Should get the available databases', function () {
expect(vm.databases).toEqual([
{
name: 'DB 1',
chosenRoles: function() { return []; },
chosenModules: function () { return []; }
},
{
name: 'DB 2',
chosenRoles: function () { return []; },
chosenModules: function () { return []; }
},
{
name: 'DB 3',
chosenRoles: function () { return []; },
chosenModules: function () { return []; }
},
{
name: 'DB 4',
chosenRoles: function () { return []; },
chosenModules: function () { return []; }
},
{
name: 'DB 5',
chosenRoles: function () { return []; },
chosenModules: function () { return []; }
}
]);
});
Summary (1 tests failed)
x User Management Initial state Should get the available databases
Expected [ { name : 'DB 1', chosenRoles : Function, chosenModules : Functi
}, { name : 'DB 2', chosenRoles : Function, chosenModules : Function }, { nam
: 'DB 3', chosenRoles : Function, chosenModules : Function }, { name : 'DB 4',
hosenRoles : Function, chosenModules : Function }, { name : 'DB 5', chosenRole
: Function, chosenModules : Function } ] to equal [ { name : 'DB 1', chosenRol
: Function, chosenModules : Function }, { name : 'DB 2', chosenRoles : Functi
, chosenModules : Function }, { name : 'DB 3', chosenRoles : Function, chosenM
ules : Function }, { name : 'DB 4', chosenRoles : Function, chosenModules : Fu
tion }, { name : 'DB 5', chosenRoles : Function, chosenModules : Function } ].原始邮政
我对进行茉莉花测试非常陌生,所以这可能是一个简单的问题,但我真的找不到与我的情况相匹配的东西。
我使用下划线在数组中创建一个由五个对象组成的列表,每个对象中有两个Knockout observableArray()。
var pfp = pfp || {};
pfp.insight = pfp.insight || {};
pfp.insight.controllers = pfp.insight.controllers || {};
(function() {
'use strict';
this.settingsController = function() {
var root = this;
root.databases = _.range(5).map(function (i) {
return {
name: 'DB ' + (i + 1),
chosenRoles: ko.observableArray(),
chosenModules: ko.observableArray()
};
});
};
}).call(pfp.insight.controllers);我不太清楚如何编写一个单元测试来检查数据库数组的初始状态。
describe('User Management', function () {
'use strict';
var vm,
databases = [];
describe('Initial state', function() {
beforeEach(function () {
vm = new pfp.insight.controllers.settingsController();
});
it('Should get the available databases', function() {
expect(vm.databases).toEqual([
{
name: 'DB 1',
chosenRoles: [],
chosenModules: []
},
{
name: 'DB 2',
chosenRoles: [],
chosenModules: []
},
{
name: 'DB 3',
chosenRoles: [],
chosenModules: []
},
{
name: 'DB 4',
chosenRoles: [],
chosenModules: []
},
{
name: 'DB 5',
chosenRoles: [],
chosenModules: []
}
]);
});
});发布于 2015-01-19 17:50:32
我想问题在于比较ko.observableArray()和[]的相等性。
expect(vm.databases.length).toEqual(5);
expect(vm.databases[0].name).toEqual('DB 1');
expect(vm.databases[0].chosenRoles.length).toEqual(0);
expect(vm.databases[0].chosenModules.length).toEqual(0);
...
expect(vm.databases[4].name).toEqual('DB 5');
expect(vm.databases[4].chosenRoles.length).toEqual(0);
expect(vm.databases[4].chosenModules.length).toEqual(0);发布于 2015-01-19 18:19:17
断言中的对象将chosenRoles和chosenModules作为javascript数组。但你把它们做成了observableArrays。这意味着它们是真正的功能。
我可能会像上面might建议的那样做,但请记住对断言进行函数调用(添加括号)(并将toEqual更改为toBe,以便获得严格的相等)。
expect(vm.databases[0].chosenRoles().length).toBe(0); expect(vm.databases[0].chosenModules().length).toBe(0);
https://stackoverflow.com/questions/28027643
复制相似问题