Skip to content

Instantly share code, notes, and snippets.

@rharriso
Last active December 16, 2015 11:29
Show Gist options
  • Save rharriso/5427434 to your computer and use it in GitHub Desktop.
Save rharriso/5427434 to your computer and use it in GitHub Desktop.
# 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()
describe "onClick", ->
it "triggers @model selected", ->
#spy
spyOn(@simpleModel, "trigger")
#trigger
@simpleView.onClick()
#expect
expect(@simpleModel.trigger).toHaveBeenCalledWith("selected")
# 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.
# 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.
#spec file
describe "simple spy", ->
it "replaces passed function", ->
dummy = -> 7
spyOn(dummy)
expect(dummy()).not.toBe(7)
#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()
#spec file
describe "spy and call through", ->
it "can call through", ->
dummy = -> 7
spyOn(dummy).andCallThrough()
expect(dummy()).toBe(7)
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