Skip to content

Instantly share code, notes, and snippets.

@mizchi
Forked from anonymous/test.coffee
Created August 7, 2012 07:22
Show Gist options
  • Save mizchi/3282775 to your computer and use it in GitHub Desktop.
Save mizchi/3282775 to your computer and use it in GitHub Desktop.
sinonの練習
sinon = require 'sinon'
# class stubbing
class MockClass
method: ->
method2: ->
sinon.stub(MockClass.prototype, 'method').withArgs(1).returns 42
obj = new MockClass
console.log obj.method(1) #=> 42
console.log obj.method(0) #=> undefined
# sinon.match
sinon.stub(MockClass.prototype, 'method2').withArgs(sinon.match.number).returns 42* 42
console.log obj.method2(424239748923) #=> 1764
# calledWithNew
class MockClass2
constructor: ->
@onNew()
onNew: ->
sinon.stub(MockClass2.prototype, 'onNew')
m2 = new MockClass2
console.log MockClass2.prototype.onNew.called #=> true
MockClass2.prototype.onNew.restore()
console.log MockClass2.prototype.onNew.called #=> undefined
#sinon.mock
class MockClass3
a: (str) -> @b(str); @b(str)
b: (str) -> str
mock = sinon.mock(MockClass3.prototype)
mock.expects('b').twice().withArgs('hoge')
m3 = new MockClass3
m3.a("hoge")
mock.verify() #=> pass test
mock.restore()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment