Skip to content

Instantly share code, notes, and snippets.

@MatthewKosloski
Last active October 16, 2016 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewKosloski/0e3a44f49e8ba39d491af644552230c7 to your computer and use it in GitHub Desktop.
Save MatthewKosloski/0e3a44f49e8ba39d491af644552230c7 to your computer and use it in GitHub Desktop.
An example of a spy and stub
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