import { TestBed } from '@angular/core/testing'; import { CalculatorService } from './calculator.service'; import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing"; import {HttpClient} from "@angular/common/http"; describe('CalculatorService', () => { let service: CalculatorService; let httpClient : HttpClient; let httpTestingController : HttpTestingController; beforeEach(() => { TestBed.configureTestingModule({ imports: [HttpClientTestingModule], }); httpClient = TestBed.inject(HttpClient); httpTestingController = TestBed.inject(HttpTestingController); service = TestBed.inject(CalculatorService); }); afterEach(() => { httpTestingController.verify(); }); it('should be created', () => { expect(service).toBeTruthy(); }); it('should return ', () => { let mockResponse : number = 3; service.calculate('add', 1, 2).subscribe( r => expect(r.body).toEqual(mockResponse, 'should return expected number'), fail ); const req = httpTestingController.expectOne('/api/numbers/1/add/2'); expect(req.request.method).toEqual('GET'); req.flush(mockResponse); }); });