jeudi 15 septembre 2016

knockout - unit testing computed observables

In knockout, i want to unit test the value of a computed observable that depends on another observable, using jasmine.

However, it doesn't work, as the value of the computed observable doesn't update when i change the other observable.

Here is my (simplified) view model:

function MarkersViewModel() {
    var self = this;   
    self.name = ko.observable("chad");
    self.computedName = ko.computed(function() {
        return self.name();
});

Here is my jasmine spec:

describe("string", function() {
    var view_model = new MarkersViewModel();
    view_model.name = ko.observable("joe");

    it("returns the whole array when there is no filter", function() {
        expect(view_model.computedName()).toBe("joe");
    });
});

When i run this, jasmine fails:

Expected 'chad' to be 'joe'.

Any idea on how i could implement that?

Thanks

Aucun commentaire:

Enregistrer un commentaire