in Education by
I have a service with a simple GET method: export class MyService { constructor(private http: HttpClient) {} getUsers() { const path = 'https://gorest.co.in/public/v2/users' return this.http.get(path) } } Next I'm injecting it into a component: export class AppComponent implements OnInit { users: any[] constructor(private _myService: MyService) {} ngOnInit() { this._myService.getUsers().subscribe((usersRes) => { this.users = usersRes }) } } I Also have a simple stub for users: const usersMock = [ { gender: 'female', id: 3971, name: 'Piotreczi Krawaczi', }, ] const myServiceStub = jest.fn().mockImplementation(() => ({ getUsers: jest.fn().mockReturnValue(of(usersMock)) }) ...and then I want to use it in TestBed in app.components.spec.ts. beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ imports: [HttpClientTestingModule], declarations: [AppComponent], providers: [{ MyService, useValue: myServiceStub }], }).compileComponents() fixture = TestBed.createComponent(AppComponent) component = fixture.componentInstance })) ...finally I want to test that, users isn't empty: it('should get Users', fakeAsync(() => { fixture.detectChanges() expect(component.users.length).toBeGreaterThan(0) flush() })) I'm getting error: TypeError: Cannot read property 'length' of undefined, whcich is related to expect(component.todos.length).toBeGreaterThan(0). Does anybody know why todos isn't updated and code in getUsers().subscribe isn't executed? I tried also useClass insead useValue with: providers: [{ MyServiceService, useClass: MyServiceStubClass }], and: class MyServiceStubClass { getTodos() { return of(todosMock) } getUsers() { return of(usersMock) } } ...but it doesn't work. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I am thinking you may need to have a tick for the subscribe to take effect before your expect. Try this: class MyServiceStubClass { getTodos() { return of(todosMock) } getUsers() { return of(usersMock) } } providers: [{ MyService, useClass: MyServiceStubClass }], .... it('should get Users', fakeAsync(() => { // first fixture.detectChanges() calls ngOnInit fixture.detectChanges(); // tick should wait for the subscribe to be completed before going to expect tick(); expect(component.users.length).toBeGreaterThan(0); flush(); }))

Related questions

0 votes
    I'm trying to write tests for an angular 5 application which uses a js import, I've been doing ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Hi Guys I'm following Max's Udemy tutorial for the MEAN stack, I have issues with Material and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    Hi Guys I'm following Max's Udemy tutorial for the MEAN stack, I have issues with Material and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    What are the three js files that you require to setup a working environment for backbone?...
asked Nov 13, 2020 in Technology by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I am receiving the error "'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I know I can do this: IDateTimeFactory dtf = MockRepository.GenerateStub(); dtf.Now = new DateTime(); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    The best stub selection for the transmission line will be (a) Series open (b) Series short ... proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 6, 2021 in Education by JackTerrance
0 votes
    I'm trying to mock a TypeScript class with Jest and I'm obviously doing something because receive the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    In my tests I need to test what happens when an OracleException is thrown (due to a stored procedure ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    A formal contract between a Service Provider (SP) and a Service Consumer (SC) is known as a. Service Level ... d. Cloud Economic Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
...