jasmine-stealth
Version:
Makes Jasmine spies a bit more robust
238 lines (188 loc) • 8.97 kB
text/coffeescript
( ->
root = this
root.context = root.describe
root.xcontext = root.xdescribe
describe "jasmine-stealth", ->
describe "aliases", ->
Then -> jasmine.createStub == jasmine.createSpy
describe ".stubFor", ->
context "existing method", ->
Given -> root.lolol = -> "roflcopter"
When -> stubFor(root, "lolol").andReturn("lol")
Then -> root.lolol() == "lol"
context "non-existing method", ->
Given -> = { woot: null }
When -> spyOn( , "woot").andReturn("troll")
Then -> .woot() == "troll"
describe "#when", ->
Given -> = jasmine.createSpy("my spy")
context "a spy is returned by then*()", ->
Then -> expect( .when("a").thenReturn("")).toBe( )
Then -> expect( .when("a").thenCallFake((->))).toBe( )
describe "#thenReturn", ->
context "the stubbing is unmet", ->
Given -> .when("53").thenReturn("yay!")
Then -> expect( ).not.toBeDefined()
context "the stubbing is met", ->
Given -> .when("53").thenReturn("winning")
Then -> == "winning"
context "multiple stubbings exist", ->
Given -> .when("pirate", booty: ["jewels", jasmine.any(String)]).thenReturn("argh!")
Given -> .when("panda", 1).thenReturn("sad")
Then -> == "argh!"
Then -> == "sad"
context "complex types", ->
Given -> =
fruits: [ "apple", "berry" ]
yogurts:
greek: ->
"expensive"
context "complex return types", ->
Given -> .when("breakfast").thenReturn( )
Then -> ==
context "complex argument types", ->
Given -> .when( ).thenReturn("breakfast")
Then -> == "breakfast"
context "stubbing with multiple arguments", ->
Given -> .when(1, 1, 2, 3, 5).thenReturn("fib")
Then -> == "fib"
context "returns a function", ->
Given -> = -> throw "WTF DUDE"
Given -> .when(1).thenReturn( )
Then -> ==
context "a stubbing is later overridden", ->
Given -> .when("foo").thenReturn(1)
context "here's that override I talked about", ->
Given -> .when("foo").thenReturn(2)
Then -> == 2
describe "#thenCallFake", ->
context "stubbing a conditional call fake", ->
Given -> = jasmine.createSpy("fake")
Given -> .when("panda", "baby").thenCallFake( )
When ->
Then -> expect( ).toHaveBeenCalledWith("panda", "baby")
context "default andReturn plus some conditional stubbing", ->
Given -> .andReturn "football"
Given -> .when("bored").thenReturn "baseball"
describe "it doesn't appear to invoke the spy", ->
Then -> expect( ).not.toHaveBeenCalled()
Then -> .callCount == 0
Then -> .calls.length == 0
Then -> .argsForCall.length == 0
Then -> expect( .mostRecentCall).toEqual({})
context "stubbing is not satisfied", ->
Then -> == "football"
context "stubbing is satisfied", ->
Then -> == "baseball"
context "default andCallFake plus some conditional stubbing", ->
Given -> .andCallFake (s1,s2) -> s2
Given -> .when("function").thenCallFake -> "football"
Given -> .when("value").thenReturn "baseball"
describe "it doesn't appear to invoke the spy", ->
Then -> expect( ).not.toHaveBeenCalled()
Then -> .callCount == 0
Then -> .calls.length == 0
Then -> .argsForCall.length == 0
Then -> expect( .mostRecentCall).toEqual({})
context "default stubbing is satisfied", ->
Then -> == "tennis"
context "conditional function stubbing is satisfied", ->
Then -> == "football"
context "conditional value stubbing is satisfied", ->
Then -> == "baseball"
describe "#whenContext", ->
Given -> = "A"
Given -> = jasmine.createSpy().whenContext( ).thenReturn("foo")
context "when satisfied", ->
When -> = .call( )
Then -> == "foo"
context "when not satisfied", ->
When -> = .call("B")
Then -> == undefined
describe "#mostRecentCallThat", ->
Given -> = jasmine.createSpy()
Given ->
Given ->
Given ->
context "when given a truth test", ->
When -> = .mostRecentCallThat (call) ->
call.args[0] is "bar"
Then -> == .calls[1]
context "when the context matters", ->
Given -> = "baz"
When -> = .mostRecentCallThat((call) ->
call.args[0] is
, this)
Then -> == .calls[2]
describe "jasmine.createStubObj", ->
context "used just like createSpyObj", ->
Given -> = jasmine.createStubObj('foo',['a','b'])
Given -> .a()
Given -> .b()
Then -> expect( .a).toHaveBeenCalled()
Then -> expect( .b).toHaveBeenCalled()
context "passed an obj literal", ->
Given -> = jasmine.createStubObj 'foo',
a: 5
b: -> 8
Then -> .a() == 5
Then -> .b() == 8
describe "jasmine.argThat (jasmine.Matchers.ArgThat)", ->
context "with when()", ->
Given -> = jasmine.createSpy()
Given -> .when(jasmine.argThat (arg) -> arg > 5).thenReturn("YAY")
Given -> .when(jasmine.argThat (arg) -> arg < 3).thenReturn("BOO")
Then -> == "BOO"
Then -> == undefined
Then -> == "YAY"
context "with a spy arg, using toHaveBeenCalledWith", ->
Given -> = jasmine.createSpy()
When ->
Then -> expect( ).toHaveBeenCalledWith(jasmine.argThat (arg) -> arg < 6)
Then -> expect( ).not.toHaveBeenCalledWith(jasmine.argThat (arg) -> arg > 5)
context "passes the equals contract", ->
Then -> true == jasmine.getEnv().equals_(5, jasmine.argThat (arg) -> arg == 5)
Then -> false == jasmine.getEnv().equals_(5, jasmine.argThat (arg) -> arg == 4)
Then -> false == jasmine.getEnv().equals_(5, jasmine.argThat (arg) -> arg != 5)
describe "jasmine.captor, #capture() & .value", ->
Given -> = jasmine.captor()
Given -> = jasmine.createSpy()
When ->
Then -> expect( ).toHaveBeenCalledWith( .capture())
And -> .value == "foo!"
it "readme example", ->
captor = jasmine.captor()
save = jasmine.createSpy()
save({ name: "foo", phone: "123"});
expect(save).toHaveBeenCalledWith(captor.capture())
expect(captor.value.name).toBe("foo")
describe "root.spyOnConstructor", ->
describe "a simple class", ->
class root.Pizza
makeSlice: -> "nah"
context "spying on the constructor - string method arg", ->
Given -> = spyOnConstructor(root, "Pizza", "makeSlice")
When -> new Pizza("banz").makeSlice("lol")
Then -> expect( .constructor).toHaveBeenCalledWith("banz")
Then -> expect( .makeSlice).toHaveBeenCalledWith("lol")
context "spying on the constructor - array method arg", ->
Given -> = spyOnConstructor(root, "Pizza", ["makeSlice"])
When -> new Pizza("banz").makeSlice("lol")
Then -> expect( .constructor).toHaveBeenCalledWith("banz")
Then -> expect( .makeSlice).toHaveBeenCalledWith("lol")
context "normal operation", ->
Given -> = new Pizza
Then -> .makeSlice() == "nah"
describe "a collaboration", ->
class root.View
serialize: ->
model: new Model().toJSON()
class root.Model
context "stubbing the model's method", ->
Given -> = spyOnConstructor(root, "Model", "toJSON")
Given -> = new root.View()
Given -> .toJSON.andReturn("some json")
When -> = .serialize()
Then -> expect( ).toEqual
model: "some json"
)()