Skip to content

Commit 2bb9053

Browse files
committed
Update calculator.service.spec.ts
1 parent 93ffc8a commit 2bb9053

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,46 @@ import { LoggerService } from "./logger.service";
33

44
// Test suit
55
describe("CalculatorService", () => {
6-
// Specification
6+
//using beforeEach to avoid code repeating for initialized which we need in more than one
7+
//test specification.
8+
// beforeEach will run before our specification
9+
10+
//to make available our variable through out the specs we need to declare here
11+
12+
let calculator: CalculatorService, loggerSpy: any;
13+
14+
beforeEach(() => {
15+
console.log("calling beforeEach");
16+
loggerSpy = jasmine.createSpyObj("LoggerService", ["log"]);
17+
calculator = new CalculatorService(loggerSpy);
18+
});
19+
20+
// Specification (and each test are independent and does not interfere with each other)
21+
// and the calculation instance that are used on spec 1 is different than the spec 2nd one.
22+
// these test should be isolated from each other
723
it("should add two number", () => {
24+
console.log("calling our first specifications");
825
// creating a fake version of logger service by creating a new instance of it
926
// const logger = new LoggerService();
1027

1128
// jasmine will take the original spied object and beside calling the original functionality, it replaces the methods with a mock one instead of using it original one
1229
// spyOn(logger, "log");
1330

1431
// if we want to do the above steps with a single line using jasmine.createSpyObj() method then
15-
const logger = jasmine.createSpyObj('LoggerService', ['log']);
16-
32+
// const logger = jasmine.createSpyObj("LoggerService", ["log"]);
1733

18-
const calculator = new CalculatorService(logger);
34+
// const calculator = new CalculatorService(logger);
1935

2036
const result = calculator.add(2, 2);
2137

2238
expect(result).toBe(4);
2339

24-
expect(logger.log).toHaveBeenCalledTimes(1);
40+
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
2541
});
2642

2743
// Specification
2844
it("should subtract two number", () => {
29-
const calculator = new CalculatorService(new LoggerService());
45+
// const calculator = new CalculatorService(new LoggerService());
3046

3147
const result = calculator.subtract(2, 2);
3248

0 commit comments

Comments
 (0)