mercredi 4 juillet 2018

Angular Testing: TestBed doesn't require providing ApplicationRef

I'm following along the official 'testing' guide at https://angular.io/guide/testing writing tests for an application.

As I'm testing a service with dependencies, I need to provide mocks for these dependencies, but interestingly this does not seem to be true for ApplicationRef, and I'd really like to know why.

The service is something like this:

export class MyService {

    constructor(
        private dependentService: DependentService,
        private applicationRef:ApplicationRef
    ){}
    ...

and the corresponding test spec:

describe('MyService', () => {
    let testAPIService: APIService;
    let dependentServiceSpy: jasmine.SpyObj<HttpClient>;

    beforeEach(() => {
        const dependentServiceSpy = jasmine.createSpyObj('DependentService', ['test']);

        TestBed.configureTestingModule({
            // Provide both the service-to-test and its (spy) dependency
            // why is 'ApplicationRef' not needed here?? 
            providers: [
                MyService,
                { provide: DependentService, useValue: dependentService_spy }
            ]
        });
    });
    ...   

});

As both 'DependentService' and 'ApplicationRef' are injected in the MyService constructor, i'd expect both are needed in the TestBed's providers array. But while leaving out 'DependentService' yields an error in tests, the missing 'ApplicationRef' does not.

Is there some reasonable explanation for this?

Aucun commentaire:

Enregistrer un commentaire