Skip to content

Commit

Permalink
Add calledImmediatelyBefore/calledImmediatelyAfter
Browse files Browse the repository at this point in the history
These are new in Sinon v2.
  • Loading branch information
domenic committed May 1, 2017
1 parent 8f8c5a0 commit f0628a1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -64,6 +64,14 @@ equivalent is also available.
<td>calledAfter</td>
<td>spy1.should.have.been.calledAfter(spy2)</td>
</tr>
<tr>
<td>calledImmediatelyBefore</td>
<td>spy.should.have.been.calledImmediatelyBefore(spy2)</td>
</tr>
<tr>
<td>calledImmediatelyAfter</td>
<td>spy.should.have.been.calledImmediatelyAfter(spy2)</td>
</tr>
<tr>
<td>calledWithNew</td>
<td>spy.should.have.been.calledWithNew</td>
Expand Down
2 changes: 2 additions & 0 deletions lib/sinon-chai.js
Expand Up @@ -122,6 +122,8 @@
sinonMethodAsProperty("calledWithNew", "been called with new");
sinonMethod("calledBefore", "been called before %1");
sinonMethod("calledAfter", "been called after %1");
sinonMethod("calledImmediatelyBefore", "been called immediately before %1");
sinonMethod("calledImmediatelyAfter", "been called immediately after %1");
sinonMethod("calledOn", "been called with %1 as this", ", but it was called with %t instead");
sinonMethod("calledWith", "been called with arguments %*", "%C");
sinonMethod("calledWithExactly", "been called with exact arguments %*", "%C");
Expand Down
69 changes: 69 additions & 0 deletions test/callOrder.coffee
Expand Up @@ -5,10 +5,12 @@ sinon = require("sinon")
describe "Call order", ->
spy1 = null
spy2 = null
spy3 = null

beforeEach ->
spy1 = sinon.spy()
spy2 = sinon.spy()
spy3 = sinon.spy()

describe "spy1 calledBefore spy2", ->
it "should throw an assertion error when neither spy is called", ->
Expand Down Expand Up @@ -36,6 +38,39 @@ describe "Call order", ->

expect(-> spy1.should.have.been.calledBefore(spy2)).to.throw(AssertionError)

describe "spy1 calledImmediatelyBefore spy2", ->
it "should throw an assertion error when neither spy is called", ->
expect(-> spy1.should.have.been.calledImmediatelyBefore(spy2)).to.throw(AssertionError)

it "should throw an assertion error when only spy 1 is called", ->
spy1()

expect(-> spy1.should.have.been.calledImmediatelyBefore(spy2)).to.throw(AssertionError)

it "should throw an assertion error when only spy 2 is called", ->
spy2()

expect(-> spy1.should.have.been.calledImmediatelyBefore(spy2)).to.throw(AssertionError)

it "should not throw when spy 1 is called immediately before spy 2", ->
spy1()
spy2()

expect(-> spy1.should.have.been.calledImmediatelyBefore(spy2)).to.not.throw()

it "should throw an assertion error when spy 1 is called before spy 2, but not immediately", ->
spy2()
spy3()
spy1()

expect(-> spy1.should.have.been.calledImmediatelyBefore(spy2)).to.throw(AssertionError)

it "should throw an assertion error when spy 1 is called after spy 2", ->
spy2()
spy1()

expect(-> spy1.should.have.been.calledImmediatelyBefore(spy2)).to.throw(AssertionError)

describe "spy1 calledAfter spy2", ->
it "should throw an assertion error when neither spy is called", ->
expect(-> spy1.should.have.been.calledAfter(spy2)).to.throw(AssertionError)
Expand All @@ -61,3 +96,37 @@ describe "Call order", ->
spy1()

expect(-> spy1.should.have.been.calledAfter(spy2)).to.not.throw()

describe "spy1 calledImmediatelyAfter spy2", ->
it "should throw an assertion error when neither spy is called", ->
expect(-> spy1.should.have.been.calledImmediatelyAfter(spy2)).to.throw(AssertionError)

it "should throw an assertion error when only spy 1 is called", ->
spy1()

expect(-> spy1.should.have.been.calledImmediatelyAfter(spy2)).to.throw(AssertionError)

it "should throw an assertion error when only spy 2 is called", ->
spy2()

expect(-> spy1.should.have.been.calledImmediatelyAfter(spy2)).to.throw(AssertionError)

it "should throw an assertion error when spy 1 is called before spy 2", ->
spy1()
spy2()

expect(-> spy1.should.have.been.calledImmediatelyAfter(spy2)).to.throw(AssertionError)

it "should not throw when spy 1 is called immediately after spy 2", ->
spy2()
spy1()

expect(-> spy1.should.have.been.calledImmediatelyAfter(spy2)).to.not.throw()

it "should throw an assertion error when spy 1 is called after spy 2, but not immediately", ->
spy1()
spy3()
spy2()

expect(-> spy1.should.have.been.calledImmediatelyAfter(spy2)).to.throw(AssertionError)

10 changes: 10 additions & 0 deletions test/messages.coffee
Expand Up @@ -62,8 +62,13 @@ describe "Messages", ->

expect(-> spyA.should.have.been.calledBefore(spyB)).to
.throw("expected spyA to have been called before function spyB() {}")
expect(-> spyA.should.have.been.calledImmediatelyBefore(spyB)).to
.throw("expected spyA to have been called immediately before function spyB() {}")

expect(-> spyB.should.have.been.calledAfter(spyA)).to
.throw("expected spyB to have been called after function spyA() {}")
expect(-> spyB.should.have.been.calledImmediatelyAfter(spyA)).to
.throw("expected spyB to have been called immediately after function spyA() {}")

it "should be correct for the negated cases", ->
spyA = sinon.spy()
Expand All @@ -77,8 +82,13 @@ describe "Messages", ->

expect(-> spyA.should.not.have.been.calledBefore(spyB)).to
.throw("expected spyA to not have been called before function spyB() {}")
expect(-> spyA.should.not.have.been.calledImmediatelyBefore(spyB)).to
.throw("expected spyA to not have been called immediately before function spyB() {}")

expect(-> spyB.should.not.have.been.calledAfter(spyA)).to
.throw("expected spyB to not have been called after function spyA() {}")
expect(-> spyB.should.not.have.been.calledImmediatelyAfter(spyA)).to
.throw("expected spyB to not have been called immediately after function spyA() {}")

describe "about call context", ->
it "should be correct for the basic case", ->
Expand Down

0 comments on commit f0628a1

Please sign in to comment.