| OLD | NEW |
| (Empty) | |
| 1 "use strict" |
| 2 |
| 3 sinon = require("sinon") |
| 4 |
| 5 describe "Messages", -> |
| 6 describe "about call count", -> |
| 7 it "should be correct for the base cases", -> |
| 8 spy = sinon.spy() |
| 9 |
| 10 expect(-> spy.should.have.been.called).to |
| 11 .throw("expected spy to have been called at least once, but it w
as never called") |
| 12 expect(-> spy.should.have.been.calledOnce).to |
| 13 .throw("expected spy to have been called exactly once, but it wa
s called 0 times") |
| 14 expect(-> spy.should.have.been.calledTwice).to |
| 15 .throw("expected spy to have been called exactly twice, but it w
as called 0 times") |
| 16 expect(-> spy.should.have.been.calledThrice).to |
| 17 .throw("expected spy to have been called exactly thrice, but it
was called 0 times") |
| 18 |
| 19 expect(-> spy.should.have.callCount(1)).to |
| 20 .throw("expected spy to have been called exactly once, but it wa
s called 0 times") |
| 21 expect(-> spy.should.have.callCount(4)).to |
| 22 .throw("expected spy to have been called exactly 4 times, but it
was called 0 times") |
| 23 |
| 24 it "should be correct for the negated cases", -> |
| 25 calledOnce = sinon.spy() |
| 26 calledTwice = sinon.spy() |
| 27 calledThrice = sinon.spy() |
| 28 calledFourTimes = sinon.spy() |
| 29 |
| 30 calledOnce() |
| 31 calledTwice() |
| 32 calledTwice() |
| 33 calledThrice() |
| 34 calledThrice() |
| 35 calledThrice() |
| 36 calledFourTimes() |
| 37 calledFourTimes() |
| 38 calledFourTimes() |
| 39 calledFourTimes() |
| 40 |
| 41 expect(-> calledOnce.should.not.have.been.called).to |
| 42 .throw("expected spy to not have been called") |
| 43 expect(-> calledOnce.should.not.have.been.calledOnce).to |
| 44 .throw("expected spy to not have been called exactly once") |
| 45 expect(-> calledTwice.should.not.have.been.calledTwice).to |
| 46 .throw("expected spy to not have been called exactly twice") |
| 47 expect(-> calledThrice.should.not.have.been.calledThrice).to |
| 48 .throw("expected spy to not have been called exactly thrice") |
| 49 |
| 50 expect(-> calledOnce.should.not.have.callCount(1)).to |
| 51 .throw("expected spy to not have been called exactly once") |
| 52 expect(-> calledFourTimes.should.not.have.callCount(4)).to |
| 53 .throw("expected spy to not have been called exactly 4 times") |
| 54 |
| 55 describe "about call order", -> |
| 56 it "should be correct for the base cases", -> |
| 57 spyA = sinon.spy() |
| 58 spyB = sinon.spy() |
| 59 |
| 60 spyA.displayName = "spyA" |
| 61 spyB.displayName = "spyB" |
| 62 |
| 63 expect(-> spyA.should.have.been.calledBefore(spyB)).to |
| 64 .throw("expected spyA to have been called before function spyB()
{}") |
| 65 expect(-> spyA.should.have.been.calledImmediatelyBefore(spyB)).to |
| 66 .throw("expected spyA to have been called immediately before fun
ction spyB() {}") |
| 67 |
| 68 expect(-> spyB.should.have.been.calledAfter(spyA)).to |
| 69 .throw("expected spyB to have been called after function spyA()
{}") |
| 70 expect(-> spyB.should.have.been.calledImmediatelyAfter(spyA)).to |
| 71 .throw("expected spyB to have been called immediately after func
tion spyA() {}") |
| 72 |
| 73 it "should be correct for the negated cases", -> |
| 74 spyA = sinon.spy() |
| 75 spyB = sinon.spy() |
| 76 |
| 77 spyA.displayName = "spyA" |
| 78 spyB.displayName = "spyB" |
| 79 |
| 80 spyA() |
| 81 spyB() |
| 82 |
| 83 expect(-> spyA.should.not.have.been.calledBefore(spyB)).to |
| 84 .throw("expected spyA to not have been called before function sp
yB() {}") |
| 85 expect(-> spyA.should.not.have.been.calledImmediatelyBefore(spyB)).t
o |
| 86 .throw("expected spyA to not have been called immediately before
function spyB() {}") |
| 87 |
| 88 expect(-> spyB.should.not.have.been.calledAfter(spyA)).to |
| 89 .throw("expected spyB to not have been called after function spy
A() {}") |
| 90 expect(-> spyB.should.not.have.been.calledImmediatelyAfter(spyA)).to |
| 91 .throw("expected spyB to not have been called immediately after
function spyA() {}") |
| 92 |
| 93 describe "about call context", -> |
| 94 it "should be correct for the basic case", -> |
| 95 spy = sinon.spy() |
| 96 context = {} |
| 97 badContext = { x: "y" } |
| 98 |
| 99 spy.call(badContext) |
| 100 |
| 101 expected = "expected spy to have been called with { } as this, but
it was called with " + |
| 102 spy.printf("%t") + " instead" |
| 103 expect(-> spy.should.have.been.calledOn(context)).to.throw(expected) |
| 104 expect(-> spy.getCall(0).should.have.been.calledOn(context)).to.thro
w(expected) |
| 105 |
| 106 it "should be correct for the negated case", -> |
| 107 spy = sinon.spy() |
| 108 context = {} |
| 109 |
| 110 spy.call(context) |
| 111 |
| 112 expected = "expected spy to not have been called with { } as this" |
| 113 expect(-> spy.should.not.have.been.calledOn(context)).to.throw(expec
ted) |
| 114 expect(-> spy.getCall(0).should.not.have.been.calledOn(context)).to.
throw(expected) |
| 115 |
| 116 it "should be correct for the always case", -> |
| 117 spy = sinon.spy() |
| 118 context = {} |
| 119 badContext = { x: "y" } |
| 120 |
| 121 spy.call(badContext) |
| 122 |
| 123 expected = "expected spy to always have been called with { } as thi
s, but it was called with " + |
| 124 spy.printf("%t") + " instead" |
| 125 expect(-> spy.should.always.have.been.calledOn(context)).to.throw(ex
pected) |
| 126 |
| 127 describe "about calling with new", -> |
| 128 it "should be correct for the basic case", -> |
| 129 spy = sinon.spy() |
| 130 |
| 131 spy() |
| 132 |
| 133 expected = "expected spy to have been called with new" |
| 134 expect(-> spy.should.have.been.calledWithNew).to.throw(expected) |
| 135 expect(-> spy.getCall(0).should.have.been.calledWithNew).to.throw(ex
pected) |
| 136 |
| 137 it "should be correct for the negated case", -> |
| 138 spy = sinon.spy() |
| 139 |
| 140 new spy() |
| 141 |
| 142 expected = "expected spy to not have been called with new" |
| 143 expect(-> spy.should.not.have.been.calledWithNew).to.throw(expected) |
| 144 expect(-> spy.getCall(0).should.not.have.been.calledWithNew).to.thro
w(expected) |
| 145 |
| 146 it "should be correct for the always case", -> |
| 147 spy = sinon.spy() |
| 148 |
| 149 new spy() |
| 150 spy() |
| 151 |
| 152 expected = "expected spy to always have been called with new" |
| 153 expect(-> spy.should.always.have.been.calledWithNew).to.throw(expect
ed) |
| 154 |
| 155 describe "about call arguments", -> |
| 156 it "should be correct for the basic cases", -> |
| 157 spy = sinon.spy() |
| 158 |
| 159 spy(1, 2, 3) |
| 160 |
| 161 expect(-> spy.should.have.been.calledWith("a", "b", "c")).to |
| 162 .throw("expected spy to have been called with arguments a, b, c\
n spy(1, 2, 3)") |
| 163 expect(-> spy.should.have.been.calledWithExactly("a", "b", "c")).to |
| 164 .throw("expected spy to have been called with exact arguments a,
b, c\n spy(1, 2, 3)") |
| 165 expect(-> spy.should.have.been.calledWithMatch(sinon.match("foo"))).
to |
| 166 .throw("expected spy to have been called with arguments matching
match(\"foo\")\n spy(1, 2, 3)") |
| 167 |
| 168 expect(-> spy.getCall(0).should.have.been.calledWith("a", "b", "c"))
.to |
| 169 .throw("expected spy to have been called with arguments a, b, c\
n spy(1, 2, 3)") |
| 170 expect(-> spy.getCall(0).should.have.been.calledWithExactly("a", "b"
, "c")).to |
| 171 .throw("expected spy to have been called with exact arguments a,
b, c\n spy(1, 2, 3)") |
| 172 expect(-> spy.getCall(0).should.have.been.calledWithMatch(sinon.matc
h("foo"))).to |
| 173 .throw("expected spy to have been called with arguments matching
match(\"foo\")\n spy(1, 2, 3)") |
| 174 |
| 175 it "should be correct for the negated cases", -> |
| 176 spy = sinon.spy() |
| 177 |
| 178 spy(1, 2, 3) |
| 179 |
| 180 expect(-> spy.should.not.have.been.calledWith(1, 2, 3)).to |
| 181 .throw("expected spy to not have been called with arguments 1, 2
, 3") |
| 182 expect(-> spy.should.not.have.been.calledWithExactly(1, 2, 3)).to |
| 183 .throw("expected spy to not have been called with exact argument
s 1, 2, 3") |
| 184 expect(-> spy.should.not.have.been.calledWithMatch(sinon.match(1))).
to |
| 185 .throw("expected spy to not have been called with arguments matc
hing match(1)") |
| 186 |
| 187 expect(-> spy.getCall(0).should.not.have.been.calledWith(1, 2, 3)).t
o |
| 188 .throw("expected spy to not have been called with arguments 1, 2
, 3") |
| 189 expect(-> spy.getCall(0).should.not.have.been.calledWithExactly(1, 2
, 3)).to |
| 190 .throw("expected spy to not have been called with exact argument
s 1, 2, 3") |
| 191 expect(-> spy.getCall(0).should.not.have.been.calledWithMatch(sinon.
match(1))).to |
| 192 .throw("expected spy to not have been called with arguments matc
hing match(1)") |
| 193 |
| 194 it "should be correct for the always cases", -> |
| 195 spy = sinon.spy() |
| 196 |
| 197 spy(1, 2, 3) |
| 198 spy("a", "b", "c") |
| 199 |
| 200 expected = /expected spy to always have been called with arguments 1
, 2, 3/ |
| 201 expect(-> spy.should.always.have.been.calledWith(1, 2, 3)).to.throw(
expected) |
| 202 |
| 203 expectedExactly = /expected spy to always have been called with exac
t arguments 1, 2, 3/ |
| 204 expect(-> spy.should.always.have.been.calledWithExactly(1, 2, 3)).to |
| 205 .throw(expectedExactly) |
| 206 |
| 207 expectedMatch = /expected spy to always have been called with argume
nts matching match\(1\)/ |
| 208 expect(-> spy.should.always.have.been.calledWithMatch(sinon.match(1)
)).to |
| 209 .throw(expectedMatch) |
| 210 |
| 211 describe "about returning", -> |
| 212 it "should be correct for the basic case", -> |
| 213 spy = sinon.spy.create(-> 1) |
| 214 |
| 215 spy() |
| 216 |
| 217 expect(-> spy.should.have.returned(2)).to.throw("expected spy to hav
e returned 2") |
| 218 expect(-> spy.getCall(0).should.have.returned(2)).to.throw("expected
spy to have returned 2") |
| 219 |
| 220 it "should be correct for the negated case", -> |
| 221 spy = sinon.spy.create(-> 1) |
| 222 |
| 223 spy() |
| 224 |
| 225 expect(-> spy.should.not.have.returned(1)).to.throw("expected spy to
not have returned 1") |
| 226 expect(-> spy.getCall(0).should.not.have.returned(1)).to.throw("expe
cted spy to not have returned 1") |
| 227 |
| 228 it "should be correct for the always case", -> |
| 229 spy = sinon.spy.create(-> 1) |
| 230 |
| 231 spy() |
| 232 |
| 233 expect(-> spy.should.always.have.returned(2)).to.throw("expected spy
to always have returned 2") |
| 234 |
| 235 describe "about throwing", -> |
| 236 it "should be correct for the basic cases", -> |
| 237 spy = sinon.spy() |
| 238 throwingSpy = sinon.spy.create(-> throw new Error()) |
| 239 |
| 240 spy() |
| 241 swallow(throwingSpy) |
| 242 |
| 243 expect(-> spy.should.have.thrown()).to.throw("expected spy to have t
hrown") |
| 244 expect(-> spy.getCall(0).should.have.thrown()).to.throw("expected sp
y to have thrown") |
| 245 |
| 246 expect(-> throwingSpy.should.have.thrown("TypeError")).to.throw("exp
ected spy to have thrown TypeError") |
| 247 expect(-> throwingSpy.getCall(0).should.have.thrown("TypeError")).to |
| 248 .throw("expected spy to have thrown TypeError") |
| 249 |
| 250 expect(-> throwingSpy.should.have.thrown({ message: "x" })).to |
| 251 .throw('expected spy to have thrown { message: "x" }') |
| 252 expect(-> throwingSpy.getCall(0).should.have.thrown({ message: "x" }
)).to |
| 253 .throw('expected spy to have thrown { message: "x" }') |
| 254 |
| 255 it "should be correct for the negated cases", -> |
| 256 error = new Error("boo!") |
| 257 spy = sinon.spy.create(-> throw error) |
| 258 |
| 259 swallow(spy) |
| 260 |
| 261 expect(-> spy.should.not.have.thrown()).to.throw("expected spy to no
t have thrown") |
| 262 expect(-> spy.getCall(0).should.not.have.thrown()).to.throw("expecte
d spy to not have thrown") |
| 263 |
| 264 expect(-> spy.should.not.have.thrown("Error")).to.throw("expected sp
y to not have thrown Error") |
| 265 expect(-> spy.getCall(0).should.not.have.thrown("Error")).to.throw("
expected spy to not have thrown Error") |
| 266 |
| 267 expect(-> spy.should.not.have.thrown(error)).to |
| 268 .throw("expected spy to not have thrown Error: boo!") |
| 269 expect(-> spy.getCall(0).should.not.have.thrown(error)).to |
| 270 .throw("expected spy to not have thrown Error: boo!") |
| 271 |
| 272 it "should be correct for the always cases", -> |
| 273 spy = sinon.spy() |
| 274 throwingSpy = sinon.spy.create(-> throw new Error()) |
| 275 |
| 276 spy() |
| 277 swallow(throwingSpy) |
| 278 |
| 279 expect(-> spy.should.have.always.thrown()).to |
| 280 .throw("expected spy to always have thrown") |
| 281 |
| 282 expect(-> throwingSpy.should.have.always.thrown("TypeError")).to |
| 283 .throw("expected spy to always have thrown TypeError") |
| 284 |
| 285 expect(-> throwingSpy.should.have.always.thrown({ message: "x" })).t
o |
| 286 .throw('expected spy to always have thrown { message: "x" }') |
| 287 |
| 288 describe "when used on a non-spy/non-call", -> |
| 289 notSpy = -> |
| 290 |
| 291 it "should be informative for properties", -> |
| 292 expect(-> notSpy.should.have.been.called).to.throw(TypeError, /not a
spy/) |
| 293 |
| 294 it "should be informative for methods", -> |
| 295 expect(-> notSpy.should.have.been.calledWith("foo")).to.throw(TypeEr
ror, /not a spy/) |
| 296 |
| 297 it "should not trigger getters for passing assertions", -> |
| 298 obj = {} |
| 299 getterCalled = false |
| 300 Object.defineProperty(obj, "getter", { |
| 301 get: -> getterCalled = true |
| 302 enumerable: true |
| 303 }) |
| 304 |
| 305 spy = sinon.spy() |
| 306 |
| 307 spy(obj) |
| 308 |
| 309 spy.should.have.been.calledWith(obj) |
| 310 |
| 311 expect(getterCalled).to.be.false |
| OLD | NEW |