Created
September 22, 2011 03:10
-
-
Save philcockfield/1233928 to your computer and use it in GitHub Desktop.
Spying into an anonymous method (Jasmine)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'spying stuff', -> | |
models = null | |
resource = null | |
beforeEach -> | |
# Fakes - just for writing the sample test. | |
# You'd use the real methods under test of course. | |
models = | |
Item: | |
findRoot: () -> console.log 'findRoot Called', err, item | |
resource = | |
index: (req, res) -> | |
models.Item.findRoot (err, item) -> | |
throw err if err? | |
res.json item.toBackbone() | |
it 'spys', -> | |
# Some sample objects that we're about to turn into spys. | |
spys = | |
item: | |
toBackbone: -> | |
req: -> | |
res: | |
json: -> | |
# Setup spys. | |
spyOn(spys.item, 'toBackbone').andCallFake () -> 'backbone:result' | |
spyOn(spys, 'req') | |
spyOn(spys.res, 'json') | |
spyOn(models.Item, 'findRoot').andCallFake (callback) -> | |
# Invoking this callback gets you inside the anonymous method. | |
callback null, spys.item | |
# Invoke the method, passing in mock params. | |
resource.index(spys.req, spys.res) | |
# Ensure everything was called as expected. | |
expect(models.Item.findRoot).toHaveBeenCalled() | |
expect(spys.res.json).toHaveBeenCalled() | |
expect(spys.res.json).toHaveBeenCalledWith('backbone:result') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment