Cheatography
https://cheatography.com
Tips for converting from Mocker to Sinon. Questions? Ask in #luna-server
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Stub/Spy Equivalents
t.mocker.installNewSpy(Foo, "bar")
|
const barSpy = t.sandbox.spy(Foo, "bar")
|
t.mocker.installNewMock(Foo, "bar")
|
const barStub = t.sandbox.stub(Foo, "bar")
|
|
Behavior Equivalents
.andReturn(foo)
|
.returns(foo)
|
If different behavior for different inputs (rare): |
.invoke(foo).andReturn(bar)
|
.withArgs(foo).returns(bar)
|
.andThrow(foo)
|
.throws(foo)
|
.andStub(fooFn)
|
.callsFake(fooFn)
|
Stub a property (not a method) (runWithReplacedGlobals) |
.value(foo)
|
|
Verification Equivalents (do you need it?)
t.mocker ...
|
t.sandbox.assert ...
|
.on().invoke()
|
No verification necessary! |
.on().invoke(arg1, arg2)
|
.alwaysCalledWithExactly(fooStub, arg1, arg2)
|
.expect().invoke()
|
.calledOnce(fooStub)
|
.expect().invoke(arg1, arg2)
|
.calledOnceWithExactly(fooStub, arg1, arg2)
|
.never()
|
.notCalled(fooSpy)
|
.once()
|
Gives .on() the behavior of .expect() |
|
Matcher Equivalents
Matcher.hasKeyValues({ foo: bar })
|
Sinon.match({ foo: bar })
|
Necessary for most StateObjects (may surface as "Maximum call stack size exceeded"): |
Matcher.hasKeyValues({ task: expected_task })
|
Sinon.match({ task: Sinon.match.same(expected_task) })
|
You can also consider matching on e.g. task.global_id |
Matcher.hasExactKeyValues({ foo: bar })
|
{ foo: bar }
|
Matcher.any
|
Sinon.match.any
|
_
|
Sinon.match.any
|
Matcher.hasSubstring("foo")
|
Sinon.match("foo")
|
Matcher.isInstanceOf(Foo)
|
Sinon.match.instanceOf(Foo)
|
Matcher.matchesRegex(/foo/)
|
Sinon.match(/foo/)
|
Matcher.isDefined()
|
Sinon.match.defined
|
Matcher.isEqual(foo, bar)
|
Sinon.match.same(foo, bar)
|
Matcher.isString()
|
Sinon.match.string
|
Matcher.isArray()
|
Sinon.match.array
|
|