多个供应商Angular 2试验台

时间:2017-03-05 19:07:52

标签: javascript unit-testing angular jasmine

我似乎无法在角度2的试验台中注册多个提供者(服务)。我想要的只是为测试平台提供多种服务。以下是我当前测试套件的示例。

export function main() {

  describe('Create Applications Component test suite:', () => {

    let fixtureComponent:any, nativeComponentElement:any;

    beforeEach(() => {

      return TestBed.configureTestingModule({
        imports: [
        ReactiveFormsModule,
        FormsModule
      ],
      declarations: [CreateApplicationComponent]
      }).overrideComponent(CreateApplicationComponent, {
        set: {
          providers: [
            {
              provide: injector,
              useClass: MockCreateApplicationService
            }
          ]
        }
      })
      .compileComponents().then(() => {
        fixtureComponent =  TestBed.createComponent(CreateApplicationComponent);
        nativeComponentElement = fixtureComponent.nativeElement;
        return fixtureComponent;
      });
    });

    it('component should work without errors', () => {
      expect(true).toBe(true);
    });

  });

}

我需要达到的目标是:

.overrideComponent(CreateApplicationComponent, {
    set: {
      providers: [
        {
          provide: injector,
          useClass: MockCreateApplicationService
        },
        {
          provide: providerB,
          useClass: SecondProvider
        }
      ]
    }
  })

1 个答案:

答案 0 :(得分:0)

原来这是添加多个提供者的正确方法。我只是因为缺少formBuilder提供程序而收到错误而感到困惑,因此我添加了该错误,现在一切都按预期工作。

溶液:

.overrideComponent(CreateApplicationComponent, {
    set: {
      providers: [
        { provide: CreateApplicationService, useClass: MockCreateApplicationService },
        { provide: UserService, useClass: MockUserService },
        { provide: FormBuilder, useClass: FormBuilder }
      ]
    }
  })
相关问题