pivotal/jasmine — Issue #160: Allow spies to be overriden
Author: mcmire
Created: 2011-11-18 19:23:49+00:00
State: open

Right now if you spy on a method more than once, Jasmine will complain. I would like it so that Jasmine did not do this. The use case is that I would like set up a default spy in my beforeEach block (as most tests in the test group depend on this) and then override this spy for individual tests where behavior differs from the default. Like so:

~~~ coffeescript
describe '#method', ->
beforeEach ->
spyOn(http, 'request').andCallFake (fn) -> fn()

it 'should do something on success', ->
# some test here
foo.method()

it 'should do something else assuming success', ->
# some test here
foo.method()

it 'should do yet another thing assuming success', ->
# some test here
foo.method()

it 'should do a different thing on error', ->
error = new Error('oh noes')
spyOn(http, 'request').andCallFake (fn) -> fn(error)
fn = jasmine.createSpy()
foo.method(fn)
expect(fn).toHaveBeenCalledWith(error)
~~~~

This is obviously a contrived example; a better example would be if the method is more complicated and I am setting up more things in my beforeEach (and have more tests). You can see how this might be useful.

WDYT?