Last active
October 13, 2020 19:01
-
-
Save waldemarnt/4965a7443ef63b09a064a0eca9f3b540 to your computer and use it in GitHub Desktop.
Fake,Spy,Stub, Mock
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
const Database = { | |
findAll() {} | |
} | |
class UsersController { | |
constructor(Database) { | |
this.Database = Database; | |
} | |
getAll() { | |
return this.Database.findAll('users'); | |
} | |
} |
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
describe('UsersController getAll()', () => { | |
it('should return a list of users', () => { | |
const expectedDatabaseResponse = [{ | |
id: 1, | |
name: 'John Doe', | |
email: '[email protected]' | |
}]; | |
const fakeDatabase = { | |
findAll() { | |
return expectedDatabaseResponse; | |
} | |
} | |
const usersController = new UsersController(fakeDatabase); | |
const response = usersController.getAll(); | |
expect(response).to.be.eql(expectedDatabaseResponse); | |
}); | |
}); |
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
describe('UsersController getAll()', () => { | |
it('should call database with correct arguments', () => { | |
const databaseMock = sinon.mock(Database); | |
databaseMock.expects('findAll').once().withArgs('users'); | |
const usersController = new UsersController(Database); | |
usersController.getAll(); | |
databaseMock.verify(); | |
databaseMock.restore(); | |
}); | |
}); |
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
describe('UsersController getAll()', () => { | |
it('should findAll users from database with correct parameters', () => { | |
const findAll = sinon.spy(Database, 'findAll'); | |
const usersController = new UsersController(Database); | |
usersController.getAll(); | |
sinon.assert.calledWith(findAll, 'users'); | |
findAll.restore(); | |
}); | |
}); |
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
describe('UsersController getAll()', () => { | |
it('should return a list of users', () => { | |
const expectedDatabaseResponse = [{ | |
id: 1, | |
name: 'John Doe', | |
email: '[email protected]' | |
}]; | |
const findAll = sinon.stub(Database, 'findAll'); | |
findAll.withArgs('users').returns(expectedDatabaseResponse); | |
const usersController = new UsersController(Database); | |
const response = usersController.getAll(); | |
sinon.assert.calledWith(findAll, 'users'); | |
expect(response).to.be.eql(expectedDatabaseResponse); | |
findAll.restore(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment