2016-09-15 42 views
0

在淘汰赛中,我想单元测试一个计算观察值的值,该值取决于另一个可观察值,使用茉莉花。淘汰赛 - 单元测试计算的observables

但是,它不起作用,因为当我更改其他observable时,计算的observable的值不会更新。

这是我(简化)视图模型:

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

这里是我的茉莉花规格:

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

当我运行这个,茉莉失败:

Expected 'chad' to be 'joe'. 

任何想法我怎么能实现呢?

感谢

回答

1

你不应该重新可观的,只是设定值:

describe("string", function() { 
    var view_model = new MarkersViewModel(); 
    view_model.name("joe"); // <- here 

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

你的计算范围的原始观察到的(在构造函数分配“乍得”),并用它。

+0

我不明白下面的代码你的评论,但它的作品。谢谢! –