Permalink
Browse files

Use Sinon–Chai for tests.

  • Loading branch information...
domenic committed Jun 6, 2012
1 parent 6f9ebf4 commit a736a02b8ce19d4d6b9e52db47ef193b10cb0fe2
View
@@ -34,6 +34,7 @@
"coffee-script": ">= 1.3.3",
"mocha": ">= 1.1.0",
"chai": ">= 1.0.4",
- "sinon": ">= 1.3.4"
+ "sinon": ">= 1.3.4",
+ "sinon-chai": ">= 2.1.0"
}
}
@@ -18,23 +18,23 @@ describe "Publisher/emitter under normal usage", ->
it "should call the subscribing listener when the event is published", ->
publisher.publish("eventName")
- sinon.assert.calledOnce(listener)
+ listener.should.have.been.calledOnce
it "should call the listener with the supplied arguments when the event is published with arguments", ->
publisher.publish("eventName", 1, "foo")
- sinon.assert.calledWithExactly(listener, 1, "foo")
+ listener.should.have.been.calledWithExactly(1, "foo")
it "should not call the listener when publishing a different event", ->
publisher.publish("anotherEvent")
- sinon.assert.notCalled(listener)
+ listener.should.not.have.been.called
it "should not call the listener when publishing after unsubscribing from the event", ->
emitter.off("eventName", listener)
publisher.publish("eventName")
- sinon.assert.notCalled(listener)
+ listener.should.not.have.been.called
describe "when an event has been subscribed to twice by the same listener", ->
listener = null
@@ -47,26 +47,26 @@ describe "Publisher/emitter under normal usage", ->
it "should call the subscribing listener twice when the event is published", ->
publisher.publish("eventName")
- listener.callCount.should.equal(2)
+ listener.should.have.been.calledTwice
it "should call the subscribing listener once when the event is unsubscribed from once, then published", ->
emitter.off("eventName", listener)
publisher.publish("eventName")
- listener.callCount.should.equal(1)
+ listener.should.have.been.calledOnce
it "should not call the subscribing listener when the event is unsubscribed from twice, then published", ->
emitter.off("eventName", listener)
emitter.off("eventName", listener)
publisher.publish("eventName")
- sinon.assert.notCalled(listener)
+ listener.should.not.have.been.called
it "should not call the subscribing listener when the event is blanket-unsubscribed, then published", ->
emitter.off("eventName")
publisher.publish("eventName")
- sinon.assert.notCalled(listener)
+ listener.should.not.have.been.called
describe "when an event has been subscribed to by two different listeners", ->
listener1 = null
@@ -82,22 +82,22 @@ describe "Publisher/emitter under normal usage", ->
it "should call both listeners when the event is published", ->
publisher.publish("eventName")
- sinon.assert.calledOnce(listener1)
- sinon.assert.calledOnce(listener2)
+ listener1.should.have.been.calledOnce
+ listener2.should.have.been.calledOnce
it "should call only one listener when the other unsubscribes, then the event is published", ->
emitter.off("eventName", listener1)
publisher.publish("eventName")
- sinon.assert.notCalled(listener1)
- sinon.assert.calledOnce(listener2)
+ listener1.should.not.have.been.called
+ listener2.should.have.been.calledOnce
it "should call neither listener when the event is blanket-unsubscribed, then published", ->
emitter.off("eventName")
publisher.publish("eventName")
- sinon.assert.notCalled(listener1)
- sinon.assert.notCalled(listener2)
+ listener1.should.not.have.been.called
+ listener2.should.not.have.been.called
describe "when three events are subscribed to in one call", ->
events = ["event1", "event2", "event3"]
@@ -110,31 +110,31 @@ describe "Publisher/emitter under normal usage", ->
it "publishes the first event correctly", ->
publisher.publish("event1")
- sinon.assert.calledOnce(listener)
+ listener.should.have.been.calledOnce
it "publishes the second event correctly", ->
publisher.publish("event2")
- sinon.assert.calledOnce(listener)
+ listener.should.have.been.calledOnce
it "publishes the third event correctly", ->
publisher.publish("event3")
- sinon.assert.calledOnce(listener)
+ listener.should.have.been.calledOnce
it "unsubscribes from two events at once correctly, when passed the listener explicitly", ->
emitter.off("event2 event3", listener)
publisher.publish("event2")
publisher.publish("event3")
- sinon.assert.notCalled(listener)
+ listener.should.not.have.been.called
it "unsubscribes from two events at once correctly, when doing blanket unsubscription", ->
emitter.off("event2 event3")
publisher.publish("event2")
publisher.publish("event3")
- sinon.assert.notCalled(listener)
+ listener.should.not.have.been.called
describe "when a hash object mapping event names to listeners is used for subscription", ->
hash = null
@@ -150,9 +150,9 @@ describe "Publisher/emitter under normal usage", ->
publisher.publish("event1")
publisher.publish("event2")
- sinon.assert.calledOnce(hash.event1)
- sinon.assert.calledOnce(hash.event2)
- sinon.assert.notCalled(hash.event3)
+ hash.event1.should.have.been.calledOnce
+ hash.event2.should.have.been.calledOnce
+ hash.event3.should.not.have.been.called
it "does not publish events when they are mass-unsubscribed using the same hash", ->
emitter.off(hash)
@@ -161,6 +161,6 @@ describe "Publisher/emitter under normal usage", ->
publisher.publish("event2")
publisher.publish("event3")
- sinon.assert.notCalled(hash.event1)
- sinon.assert.notCalled(hash.event2)
- sinon.assert.notCalled(hash.event3)
+ hash.event1.should.not.have.been.called
+ hash.event2.should.not.have.been.called
+ hash.event3.should.not.have.been.called
@@ -25,17 +25,17 @@ describe "Publisher/emitter in unusual situations", ->
it "should call all listeners when the event is published once", ->
publisher.publish("eventName")
- sinon.assert.calledOnce(normalListener1)
- sinon.assert.calledOnce(normalListener2)
- sinon.assert.calledOnce(selfRemovingListener)
+ normalListener1.should.have.been.calledOnce
+ normalListener2.should.have.been.calledOnce
+ selfRemovingListener.should.have.been.calledOnce
it "should only call the normal listeners when the event is published a second time", ->
publisher.publish("eventName")
publisher.publish("eventName")
- normalListener1.callCount.should.equal(2)
- normalListener2.callCount.should.equal(2)
- selfRemovingListener.callCount.should.equal(1)
+ normalListener1.should.have.been.calledTwice
+ normalListener2.should.have.been.calledTwice
+ selfRemovingListener.should.have.been.calledOnce
describe "when an event has been subscribed to by two normal listeners plus a listener that throws an error", ->
normalListener1 = null
@@ -57,22 +57,22 @@ describe "Publisher/emitter in unusual situations", ->
it "should call all listeners when the event is published, despite the error", ->
publisher.publish("eventName")
- sinon.assert.calledOnce(normalListener1)
- sinon.assert.calledOnce(normalListener2)
- sinon.assert.calledOnce(errorThrowingListener)
+ normalListener1.should.have.been.calledOnce
+ normalListener2.should.have.been.calledOnce
+ errorThrowingListener.should.have.been.calledOnce
it 'gracefully deals with events named "hasOwnProperty"', ->
listener = sinon.spy()
emitter.on("hasOwnProperty", listener)
publisher.publish("hasOwnProperty")
- sinon.assert.calledOnce(listener)
+ listener.should.have.been.calledOnce
it 'gracefully deals with events named "__proto__"', ->
listener = sinon.spy()
emitter.on("__proto__", listener)
publisher.publish("__proto__")
- sinon.assert.calledOnce(listener)
+ listener.should.have.been.calledOnce
View
@@ -20,7 +20,7 @@ describe "Publisher options", ->
process.nextTick ->
process.listeners("uncaughtException").push(originalOnUncaughtException)
- sinon.assert.calledWith(onUncaughtException, error)
+ onUncaughtException.should.have.been.calledWith(error)
next()
it "should deliver errors thrown by listeners to the supplied callback", ->
@@ -33,7 +33,7 @@ describe "Publisher options", ->
publisher.publish("eventName")
- sinon.assert.calledWith(onListenerError, error)
+ onListenerError.should.have.been.calledWith(error)
describe "async", ->
beforeEach ->
@@ -49,12 +49,12 @@ describe "Publisher options", ->
publisher.publish("event1")
publisher.publish("event2")
- sinon.assert.notCalled(listener1)
- sinon.assert.notCalled(listener2)
+ listener1.should.not.have.been.called
+ listener2.should.not.have.been.called
process.nextTick ->
- sinon.assert.called(listener1)
- sinon.assert.called(listener2)
+ listener1.should.have.been.called
+ listener2.should.have.been.called
next()
describe "events", ->
@@ -71,8 +71,8 @@ describe "Publisher options", ->
publisher.publish("event1")
publisher.publish("event2")
- sinon.assert.called(listener1)
- sinon.assert.called(listener2)
+ listener1.should.have.been.called
+ listener2.should.have.been.called
it "should throw an error upon attempting to subscribe to an unknown event", ->
(-> emitter.on("unknownEvent", ->)).should.throw()
Oops, something went wrong.

0 comments on commit a736a02

Please sign in to comment.