ascendantofrain
ascendantofrain

Reputation: 302

Jasmine Test Knockout ObservableArray

UPDATED

I've since updated my test and the error message coming back is very confusing...

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

Original Post

I am very new to running Jasmine Tests so this may be an easy questions but I am not really able to find anything that matches my situation.

I am using underscore to create a list of five objects in an array where in each object rests two 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);

I am not exactly sure how to write a unit test that checks the initial state of the databases array.

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: []
            }
        ]);
    });
});

Upvotes: 0

Views: 1139

Answers (2)

M Falanga
M Falanga

Reputation: 2077

The objects in your assertion have chosenRoles and chosenModules as javascript arrays. But you are making them as observableArrays. That means that they are really functions.

I might do what Mike Griffith suggests above, but remember to make the function call (add the parentheses) for the assertions (and change toEqual to toBe so that you get strict equality).

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

Upvotes: 1

Mike Griffith
Mike Griffith

Reputation: 1211

I would guess the problem lies in comparing equality for ko.observableArray()s vs. []s. Perhaps you could break your test assertions up more along the lines of:

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

Upvotes: 2

Related Questions