Skip to content

Instantly share code, notes, and snippets.

@paritosh64ce
Last active April 19, 2018 17:03
Show Gist options
  • Save paritosh64ce/aaa8fc4d9d1586af7d86c2d22027150e to your computer and use it in GitHub Desktop.
Save paritosh64ce/aaa8fc4d9d1586af7d86c2d22027150e to your computer and use it in GitHub Desktop.
TDD Angular component with Jasmine using @angular/cli
// 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