Skip to content

Instantly share code, notes, and snippets.

@MatLang
Created June 19, 2019 10:35
Show Gist options
  • Save MatLang/c8f5b7939b88e0718925f1cd44d39629 to your computer and use it in GitHub Desktop.
Save MatLang/c8f5b7939b88e0718925f1cd44d39629 to your computer and use it in GitHub Desktop.
Service testing #testing
describe('CalculatorService', () => {
let calculator: CalculatorService,
loggerSpy: any;
beforeEach(()=> {
console.log("Calling beforeEach");
loggerSpy = jasmine.createSpyObj('LoggerService', ["log"]);
TestBed.configureTestingModule({
providers: [
CalculatorService,
{provide: LoggerService, useValue: loggerSpy}
]
});
calculator = TestBed.get(CalculatorService);
});
it('should add two numbers', () => {
console.log("add test");
const result = calculator.add(2, 2);
expect(result).toBe(4);
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
});
it('should subtract two numbers', () => {
console.log("subtract test");
const result = calculator.subtract(2, 2);
expect(result).toBe(0, "unexpected subtraction result");
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment