Last active
September 8, 2017 20:02
-
-
Save kylewhitaker/9c8edaa4181a2a7f6c173d3d09720ea8 to your computer and use it in GitHub Desktop.
Example: Unit test failure due to undefined spy test double!
This file contains 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 { SomeComponent } from './some.component'; | |
describe('SomeComponent:', () => { | |
let component: SomeComponent; | |
let mockDepOne: any; | |
beforeEach(() => { | |
mockDepOne = { | |
doSomething: () => '0123456789' // We correctly define a return value for our mock... | |
}; | |
component = new SomeComponent(mockDepOne); | |
}); | |
describe('someMethod', () => { | |
// THIS TEST WILL FAIL DUE TO AN UNDEFINED SPY TEST DOUBLE | |
it('should call depOne.doSomething', () => { | |
// ...however, this spy acts a test double with no return value for 'doSomething'. | |
spyOn(mockDepOne, 'doSomething'); | |
// When calling 'someMethod', an error is thrown when calling .slice(2, 5) on an undefined return value from 'doSomething'. | |
component.someMethod(); | |
// The expect did not fail the test! The undefined return value when calling 'doSomething' failed our test as a side effect. | |
expect(mockDepOne.doSomething).toHaveBeenCalled(); | |
}); | |
}); | |
}); |
This file contains 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 { DepOne } from './dependencies'; | |
export class SomeComponent { | |
constructor(private depOne: DepOne) { } | |
someMethod(): void { | |
this.depOne.doSomething().slice(2, 5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment