How override Provider in Angular 5 for only one test?

前端 未结 5 1190
礼貌的吻别
礼貌的吻别 2020-12-17 08:24

In one of my unit test files, I have to mock several times the same service with different mocks.

import { MyService } from \'../services/myservice.service\'         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 08:31

    Just for reference, if annynone meets this issue.

    I tried to use

    TestBed.overrideProvider(MockedService, {useValue: { foo: () => {} } });
    

    it was not working, still the original service was injected in test (that with providedIn: root)

    In test I used alias to import OtherService:

    import { OtherService } from '@core/OtherService'`
    

    while in the service itself I had import with relative path:

    import { OtherService } from '../../../OtherService'
    

    After correcting it so both test and service itself had same imports TestBed.overrideProvider() started to take effect.

    Env: Angular 7 library - not application and jest

提交回复
热议问题