如果作为单元测试运行,则不会自动订阅Ngxs操作

时间:2019-03-01 10:41:50

标签: angular unit-testing ngxs

我有一个操作,该操作处理对服务器的请求。虽然在部署应用程序时一切正常,但是如果我运行单元测试,它将停止工作。 一段时间后,我发现该动作未被订阅,这通常是发生的。 我正在使用Angular的TestBed。正在分派CreateContact Action,服务的create方法返回一个可观察值,该值发出一个值。

您有任何想法吗,可能是什么问题?

有问题的操作

@Action(CreateContactPageActions.CreateContact)
public createContact(
  ctx: StateContext<ContactStateModel>,
  action: CreateContactPageActions.CreateContact
): Observable<Contact> {
  ctx.patchState({
    error: null
  });

  return this.contactService.create(action.payload)
    .pipe(
      tap(contact => {
        ctx.dispatch(new ContactStateActions.CreateContactSuccess(contact));
      }),
      catchError(err => {
        ctx.dispatch(new ContactStateActions.CreateContactFailed(err));
        return EMPTY;
      }),
    );
}

TestBench的配置

TestBed.configureTestingModule({
  imports     : [
    TranslateModule,
    FormsModule,
    BrowserModule,
    HttpClientTestingModule,
    TranslateModule.forRoot({
      loader: {
        provide : TranslateLoader,
        useClass: TranslateFakeLoader
      }
    }),
    NgxsModule.forRoot([]),
    NgxsModule.forFeature([ContactState]),
  ],
  declarations: [
    CreateContactPageComponent,
    ContactFormComponent,
    FormFieldErrorsComponent,
    ToFieldValidationErrorMessagesPipe,
  ],
  providers   : [

    {provide: Router, useValue: routerMock},
    {provide: ContactService, useClass: ContactServiceMock},
    Actions,
  ],
}).compileComponents();

0 个答案:

没有答案