August 2016
Intermediate to advanced
635 pages
14h 5m
English
The final type of fake object is a mock. The difference between a mock and a stub is where the verification is done. With a stub, our test must check if the state is correct after the act. With a mock object, the responsibility for testing the asserts falls to the mock itself. Mocks are another place where it is useful to leverage a mocking library. We can, however, build the same sort of thing, simply, ourselves:
class MockCredentialFactory {
constructor() {
this.timesCalled = 0;
}
Create() {
this.timesCalled++;
}
Verify() {
assert(this.timesCalled == 3);
}
}This mockCredentialsFactory class takes on the responsibility of verifying the correct functions were called. This is a very simple sort of approach to mocking and can be used as such: ...