added additional spy example to README.

This commit is contained in:
ragaskar 2009-03-01 07:06:09 -08:00
parent 6ee92179d8
commit 3d1fb9bc3a
1 changed files with 25 additions and 0 deletions

View File

@ -284,6 +284,31 @@ Here are a few examples:
expect(callback).wasCalledWith('foo');
});
Spies can be very useful for testing AJAX or other asynchronous behaviors that take callbacks by faking the method firing an async call.
var Klass = function () {
};
var Klass.prototype.asyncMethod = function (callback) {
someAsyncCall(callback);
};
...
it('should test async call') {
spyOn(Klass, 'asyncMethod');
var callback = Jasmine.createSpy();
Klass.asyncMethod(callback);
expect(callback).wasNotCalled();
var someResponseData = 'foo';
Klass.asyncMethod.mostRecentCall.args[0](someResponseData);
expect(callback).wasCalledWith(someResponseData);
});
There are spy-specfic matchers that are very handy.
`wasCalled()` returns true if the object is a spy and was called