Skip to content

Commit e718b5b

Browse files
author
Your Name
committed
Angular Testing Course
1 parent 5f9e25f commit e718b5b

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
import {CalculatorService} from './calculator.service';
22
import {LoggerService} from './logger.service';
3+
import {TestBed} from '@angular/core/testing';
34

45

56
describe('CalculatorService', () => {
67

8+
let calculator: CalculatorService,
9+
loggerSpy: any;
710

8-
it('should add two numbers', () => {
11+
beforeEach(()=> {
912

10-
const calculator = new CalculatorService(new LoggerService());
13+
console.log("Calling beforeEach");
14+
15+
loggerSpy = jasmine.createSpyObj('LoggerService', ["log"]);
16+
17+
TestBed.configureTestingModule({
18+
providers: [
19+
CalculatorService,
20+
{provide: LoggerService, useValue: loggerSpy}
21+
]
22+
});
23+
24+
calculator = TestBed.get(CalculatorService);
25+
26+
});
27+
28+
fit('should add two numbers', () => {
29+
30+
console.log("add test");
1131

1232
const result = calculator.add(2, 2);
1333

1434
expect(result).toBe(4);
1535

36+
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
37+
1638
});
1739

1840

1941
it('should subtract two numbers', () => {
2042

21-
const calculator = new CalculatorService(new LoggerService());
43+
console.log("subtract test");
2244

2345
const result = calculator.subtract(2, 2);
2446

2547
expect(result).toBe(0, "unexpected subtraction result");
2648

49+
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
50+
2751
});
2852

2953
});

src/app/courses/services/calculator.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class CalculatorService {
1919

2020
subtract(n1: number, n2:number) {
2121
this.logger.log("Subtraction operation called");
22-
return n1 * n2;
22+
return n1 - n2;
2323
}
2424

2525

0 commit comments

Comments
 (0)