Last active
December 20, 2017 20:42
-
-
Save cakeinpanic/79e3383f91e402f0ed353a78ae5bae4c to your computer and use it in GitHub Desktop.
dependency
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
class MyService { | |
sayHello(name: string) { | |
return 'Hello ' + name; | |
} | |
} | |
class MyComponent { | |
constructor(private myService) { | |
} | |
bePolite() { | |
return this.myService.sayHello(this.name); | |
} | |
} | |
// как только мы поменяем реализацию sayHello, придется менять и этот тест, | |
// хотя логика компонента не менялась | |
it('component is polite', () => { | |
const myService = new MyService(); | |
const myTestComponent = new MyComponent(myService); | |
myTestComponent.name = 'Ivan'; | |
expect(myTestComponent.bePolite()).toBe('Hello Ivan'); | |
}); | |
// хотя можно написать тест таким образом, тогда не сломается | |
// но тогда и реализация MyService не важна и можно смело использовать мок | |
it('component is polite', () => { | |
const myService = new MyService(); | |
const myTestComponent = new MyComponent(myService); | |
myTestComponent.name = 'Ivan'; | |
expect(myTestComponent.bePolite()).toBe(myService.sayHello(myTestComponent.name)); | |
}); | |
// хотя можно написать тест таким образом, тогда не сломается | |
// но тогда и реализация MyService не важна и можно смело использовать мок | |
it('component is polite', () => { | |
const mockHello = 'some string'; | |
const myService = {sayHello: () => mockHello}; | |
const myTestComponent = new MyComponent(myService); | |
expect(myTestComponent.bePolite()).toBe(mockHello); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment