Created
February 3, 2020 13:26
-
-
Save PhilippMeissner/a96c2686cad6214dc3261b3b48162c98 to your computer and use it in GitHub Desktop.
munchkin possible async solution - https://stackoverflow.com/questions/60038800/nullinjectorerror-staticinjectorerroraviorbackendservice-nullinjectorerror/60038952#60038952
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; | |
import {inject, TestBed} from '@angular/core/testing'; | |
import {AviorBackendService} from './avior-backend.service'; | |
describe('AviorBackendService', () => { | |
beforeEach(() => TestBed.configureTestingModule({ | |
imports: [HttpClientTestingModule], | |
providers: [AviorBackendService], | |
})); | |
it('should be created', () => { | |
const service: AviorBackendService = TestBed.get(AviorBackendService); | |
expect(service).toBeTruthy(); | |
}); | |
// Inject the `done` method. This will tell the test suite that asynchronous methods are being called | |
// and it will mark the test as failed if within a specific timeout (usually 5s) the `done` is not called | |
it('expects service to fetch data with proper sorting', (done) => { | |
const service: AviorBackendService = TestBed.get(AviorBackendService); | |
// tslint:disable-next-line: prefer-const | |
let httpMock: HttpTestingController; | |
service.getUserCollection().subscribe(data => { | |
expect(data.length).toBe(7); | |
const req = httpMock.expectOne('http://localhost:3000/users'); | |
expect(req.request.method).toEqual('GET'); // Then we set the fake data to be returned by the mock | |
req.flush({firstname: 'Chad'}); | |
done(); // Mark the test as done | |
}, done.fail); // Mark the test as failed if something goes wrong | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment