how do I mock out http post in Angular without using TestBed?

前端 未结 3 1905
一向
一向 2021-01-17 05:25

How do I unit test this login function, specifically the http post part? The http mock I made is not coded correctly to get into the \'if...else\' section of the code. I d

3条回答
  •  独厮守ぢ
    2021-01-17 05:41

    You can inject services into your test like

    it('should do something',inject([YourService,XHRBackend],(service:YourService,mockBackend)=>{
     mockBackend.connections.subscribe((connection)=>{
       connection.mockRespond(new Response({body:JSON.stringify('your mock response')});
    

    ...

    this will cause your post call to return with that mock data. What I am not certain of though is if you can actually inject XHRBackend without first doing a testbed and providing

    {provide: XHRBackend, useClass:  MockBackend}
    

提交回复
热议问题