I'm trying to write a test for a ko.computed field that is dependent on two other complex computeds (removed here for demonstration).
function PositionsViewModel(options) {
var self = this;
self.computed1 = ko.computed(function() { return 1; });
self.computed2 = ko.computed(function() { return 2; });
self.computedIWantToTest = ko.computed(function() {
return self.computed1() + self.computed2();
});
}In my jasmine test I create an instance of the VM in a beforeEach like so:
this.subject = new PositionsViewModel();I want to be able to stub computed1 and computed2 so that I may test computedIWantToTest in isolation. So far my attempts to do so have failed.
What I'd like to do:
spyOn(subject,computed2).andReturn(5);
subject.computedIWantToTest.update(true);
It appears that dependentObservables at one point computed has a notifySubscribers method, but it was removed when computeds were introduced. If that still existed I could do:
var originalComputed = subject.computed2;
spyOn(subject,computed2).andReturn(5);
subject.originalComputed.notifySubscribers(true);
Is there a better way to do this?
Ok, so I got this to work, though I hate myself for this. I really think Knockout should expose the evaluateImmediately method publicly, and I'm going to link a pull to do so.
Here's my solution:
p = new PositionsViewModel();
originalComputed = p.redApples;
spyOn(p,'computed1').andReturn(5);
originalComputed.notifySubscribers();
expect(p. computedIWantToTest()).toBe(7);Does valueHasMutated apply here?
http://stackoverflow.com/questions/8537397/knockout-js-how-to-force-view-refresh-instead-of-using-ko-observable
--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.