Last active
December 16, 2015 11:29
-
-
Save rharriso/5427434 to your computer and use it in GitHub Desktop.
Jasmine Tips - http://c0d3it.blogspot.com/2013/04/jasmine-tips-spies.html
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
# model file | |
class SimpleModel extends Backbone.Model | |
# view file | |
class SimpleView extends Backbone.View | |
# click listener | |
onClick: -> | |
@model.trigger("selected") | |
# spec file | |
describe "SimpleView", -> | |
beforeEach -> SimpleModel() | |
@simpleModel = new SimpleModel() | |
@simpleView = new SimpleView(model: @simpleModel) | |
describe "onClick", -> | |
it "triggers @model selected", -> | |
#spy | |
@listener = -> | |
spyOn(@, "listener") | |
#bind | |
@simpleModel.on("selected", @listener) | |
#trigger | |
@simpleView.onClick() | |
#expect | |
expect(@listener).toHaveBeenCalled() | |
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 "onClick", -> | |
it "triggers @model selected", -> | |
#spy | |
spyOn(@simpleModel, "trigger") | |
#trigger | |
@simpleView.onClick() | |
#expect | |
expect(@simpleModel.trigger).toHaveBeenCalledWith("selected") | |
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
# model file | |
class SimpleModel extends Backbone.Model | |
initialize: -> | |
@on("selected", @onSelected) | |
onSelected: => | |
#spec file | |
describe "SimpleModel", -> | |
beforeEach -> @simpleModel = new SimpleModel() | |
it "calls onSelected when selected is triggered", -> | |
spyOn(@simpleModel.onSelected) | |
@simpleModel.trigger("selected") | |
expect(@simpleModel.onSelected).toHaveBeenCalled() # this expectation will fail. |
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
# model file | |
class SimpleModel extends Backbone.Model | |
initialize: -> @bindEvents() | |
bindEvents: -> | |
@on("selected", @onSelected) | |
onSelected: => | |
#spec file | |
describe "SimpleModel", -> | |
beforeEach -> @simpleModel = new SimpleModel() | |
it "calls onSelected when selected is triggered", -> | |
spyOn(@simpleModel.onSelected) | |
@simpleModel.bindEvents() | |
@simpleModel.trigger("selected") | |
expect(@simpleModel.onSelected).toHaveBeenCalled() # this expectation will pass. |
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
#spec file | |
describe "simple spy", -> | |
it "replaces passed function", -> | |
dummy = -> 7 | |
spyOn(dummy) | |
expect(dummy()).not.toBe(7) | |
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
#view file | |
class SimpleView extends Backbone.View | |
onFormSubmit: (e)=> | |
e.preventDefault() | |
@model.save | |
error: @onSaveError | |
success: @onSaveSuccess | |
onSaveError: => | |
onSaveSuccess: => | |
#spec file | |
describe "SimpleView", -> | |
beforeEach -> | |
@simpleModel = new SimpleModel() | |
@simpleView = new SimpleView(model: @simpleModel) | |
describe "onFormSubmit" | |
before -> @e = {preventDefault -> } | |
it "calls onsave error when error", -> | |
spyOn(@simpleView, "onSaveError") | |
#trigger error callback | |
spyOn(@simpleModel, "save").andCallFake (obj)-> obj.error() | |
@simpleView.onFormSubmit(@e) | |
expect(@simpleView.onSaveError).toHaveBeenCalled() | |
it "calls onsave success when success", -> | |
spyOn(@simpleView, "onSaveSuccess") | |
#trigger success callback | |
spyOn(@simpleModel, "save").andCallFake (obj)-> obj.success() | |
@simpleView.onFormSubmit(@e) | |
expect(@simpleView.onSaveSuccess).toHaveBeenCalled() | |
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
#spec file | |
describe "spy and call through", -> | |
it "can call through", -> | |
dummy = -> 7 | |
spyOn(dummy).andCallThrough() | |
expect(dummy()).toBe(7) |
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 "spy and return", -> | |
it "can also be told to return a specific value", -> | |
dummy = -> 7 | |
spyOn(dummy).andReturn(17) | |
expect(dummy()).toBe(17) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment