【问题标题】:Jasmine Test Knockout ObservableArrayJasmine 测试淘汰 ObservableArray
【发布时间】:2015-01-19 15:06:50
【问题描述】:

更新

我已经更新了我的测试并且返回的错误消息非常令人困惑......

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 } ].

原帖

我对运行 Jasmine 测试非常陌生,所以这可能是一个简单的问题,但我真的找不到任何符合我情况的东西。

我正在使用下划线在一个数组中创建一个包含五个对象的列表,其中每个对象包含两个 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: []
            }
        ]);
    });
});

【问题讨论】:

    标签: javascript unit-testing knockout.js jasmine automated-tests


    【解决方案1】:

    我猜问题在于比较ko.observableArray()s 与[]s 的相等性。也许您可以按照以下方式分解您的测试断言:

    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);
    

    【讨论】:

      【解决方案2】:

      您的断言中的对象具有chosenRoleschosenModules 作为javascript 数组。但是您将它们制作为 observableArrays。这意味着它们确实是函数。

      我可能会按照 Mike Griffith 上面的建议进行操作,但请记住为断言进行函数调用(添加括号)(并将 toEqual 更改为 toBe,以便获得严格的相等性)。

      expect(vm.databases[0].chosenRoles().length).toBe(0); expect(vm.databases[0].chosenModules().length).toBe(0);

      【讨论】:

        猜你喜欢
        • 2017-10-23
        • 1970-01-01
        • 2014-10-28
        • 2012-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多