Reputation: 137
I have class SomeClass with method SomeClass.fetch()
var repository = {
get: function(obj) {
//...
}
};
var cache = false;
var SomeClass = {
init: function() {
//...
},
fetch: function () {
var _this = this;
repository.get({
method: 'getRecentDialogsList',
success: function (result) {
if (!cache) {
_this.set(result);
_this.sort();
_this.trigger('fetch:success', _this);
}
_this.trigger('fetch:ajaxSuccess', _this);
}
});
}
}
How i can test whether SomeClass.fetch()
and check have been called this.set()
, this.sort
, this.trigger
with parameters?
Upvotes: 4
Views: 8837
Reputation: 10342
You must use spyes:
describe("SomeClass Test", function() {
it("calls the set() method when fetch is called", function() {
spyOn(SomeClass, "set");
SomeClass.fetch();
expect(SomeClass.set).toHaveBeenCalled();
});
});
You can even totally replace the called method (for example, if it takes a long time to be completed) with something like this:
spyOn(SomeClass, "set").and.callFake(function myFakeSet() {
console.log("I've been called");
});
Upvotes: 3