How to fix beforeEachProviders (deprecated on RC4)

前端 未结 3 699
后悔当初
后悔当初 2021-01-17 08:58

Ive just upgraded Angular2 from RC3 to RC4 ...

import {
  expect, it, iit, xit,
  describe, ddescribe, xdescribe,
  beforeEach, beforeEachProviders, withProv         


        
相关标签:
3条回答
  • You will need to import addProviders from @angular/core/testing.

    Instead of:

    beforeEachProviders(() => [
        {provide: Router, useClass: MockRouter}
    ]);
    

    You'll want to do this:

    beforeEach(() => {
        addProviders([
            {provide: Router, useClass: MockRouter}
        ])
    });
    

    Source: RC4 Changelog

    0 讨论(0)
  • 2021-01-17 09:49

    After reviewing a few other documents, it appears you want:

    beforeEach(() => TestBed.configureTestingModule({
            providers: [
                { provide: Service, useClass: MockService }
            ]})
        );
    

    Source: https://angular.io/guide/dependency-injection

    0 讨论(0)
  • 2021-01-17 09:49

    Here's a complete example, for a Window reference service:

    import { TestBed, inject } from '@angular/core/testing';
    import { WindowRef } from './window-ref';
    
    describe('WindowRef', () => {
      let subject: WindowRef;
    
      beforeEach(() => {
        TestBed.configureTestingModule({
          providers: [
            WindowRef
          ]});
      });
    
      beforeEach(inject([WindowRef], (windowRef: WindowRef) => {
        subject = windowRef;
      }));
    
      it('should provide a way to access the native window object', () => {
        expect(subject.nativeWindow).toBe(window);
      });
    });
    
    0 讨论(0)
提交回复
热议问题