From 3d1fb9bc3a513c0a356c7d0c3cfd9a298835bc37 Mon Sep 17 00:00:00 2001 From: ragaskar Date: Sun, 1 Mar 2009 07:06:09 -0800 Subject: [PATCH] added additional spy example to README. --- README.markdown | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.markdown b/README.markdown index 37cbc648..ee3122f2 100644 --- a/README.markdown +++ b/README.markdown @@ -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