-
-
Save paritosh64ce/aaa8fc4d9d1586af7d86c2d22027150e to your computer and use it in GitHub Desktop.
TDD Angular component with Jasmine using @angular/cli
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
// update TestBed configuration to use MathService as one of the providers | |
// in the DESCRIBE section | |
beforeEach(async(() => { | |
TestBed.configureTestingModule({ | |
declarations: [ CalculatorComponent ], | |
providers: [MathService] // <=== add this line | |
}) | |
.compileComponents(); | |
})); | |
// adding another test besides IT code block | |
it('should call mathService to do subtraction', () => { | |
const mathSvcInstance = TestBed.get(MathService); | |
// Remember - what MathService SHOULD return, that's none of you business right now. | |
// you are testing calculator component - Separation of Concerns | |
spyOn(mathSvcInstance, 'subtract').and.returnValue(500); | |
component.param1 = 5; | |
component.param2 = 7; | |
component.subtract(); | |
expect(mathSvcInstance.subtract).toHaveBeenCalledWith(component.param1, component.param2); | |
expect(component.result).toBe(500); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment