Last active
October 16, 2016 02:32
-
-
Save MatthewKosloski/0e3a44f49e8ba39d491af644552230c7 to your computer and use it in GitHub Desktop.
An example of a spy and stub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import expect from 'expect'; | |
import sinon from 'sinon'; | |
import fetch from 'isomorphic-fetch'; | |
describe.only('Sinon samples', () => { | |
describe('spy', () => { | |
let spy; | |
before(() => { | |
let add = (...args) => args.reduce((a, b) => a + b); | |
spy = sinon.spy(add); | |
}) | |
it('Should add two numbers', () => { | |
spy(2, 3); | |
expect(spy.withArgs(2, 3).calledOnce).toBe(true); | |
expect(spy.returned(5)).toBe(true); | |
}); | |
}); | |
describe('stub', () => { | |
let stub, object, expected; | |
before(() => { | |
object = {}; | |
object.method = () => { | |
return fetch('https://jsonplaceholder.typicode.com/users') | |
.then((response) => response.json()) | |
.then((data) => data); | |
} | |
stub = sinon.stub(object, 'method'); | |
expected = [{id: 1, name: 'Matt'}]; | |
stub.returns(expected); | |
}); | |
it('Should return the expected data', (done) => { | |
Promise.resolve(object.method()).then((actual) => { | |
expect(actual).toEqual(expected); | |
done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment