可观察到的内管角8茉莉花的测试结果

问题描述 投票:0回答:1

我的组件中的主题已订阅triggerRuleExecutionService,该主题正从另一个组件中发出,即next()

并且在管道内部,我正在使用switchMap来调用http服务以从数据库中获取数据

 this.ruleExecutionService = this.editCheckSVC.triggerRuleExecutionService.pipe(
      switchMap(res => {
        return this.editCheckSVC.executeRules(res);
      })
    ).subscribe(res => {
      console.log(res);
    });

上面的代码在ngOnInit内部

下面是我测试上述功能的规格。

    const ruleExecutionSubject = new Subject();

    class EditChkManagementServiceStub {
      triggerRuleExecutionService = ruleExecutionSubject.asObservable();
      executeRules() {
        return of([])
      }
   }



describe('EditcheckManagmentComponent', () => {
      let component: EditcheckManagmentComponent;
      let fixture: ComponentFixture<EditcheckManagmentComponent>;
      let debugElement: DebugElement;
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [EditcheckManagmentComponent],
          schemas: [NO_ERRORS_SCHEMA],
          providers: [{ provide: EditCheckManagementService, useClass: EditChkManagementServiceStub }, HttpService],
          imports: [HttpClientModule]
        })
          .compileComponents();
      }));

      beforeEach(() => {

        fixture = TestBed.createComponent(EditcheckManagmentComponent);
        component = fixture.componentInstance;
        debugElement = fixture.debugElement;
        fixture.detectChanges();
      });


    it('should call rule execution API', () => {
        ruleExecutionSubject.next({
          formName: '',
          schedule: '',
          subSchedule: '',
          version: '',
          fiscalYear: '2018',
          accountingPeriod: '6'
        });

        fixture.detectChanges();
        fixture.whenStable().then(() => {
          const executionServiceInstance: EditCheckManagementService = TestBed.get(EditCheckManagementService);
          spyOn(executionServiceInstance, 'executeRules').and.callThrough();
          component.ngOnInit()
          expect(executionServiceInstance.executeRules).toHaveBeenCalled();
        });

      });
    });

测试用例失败,并显示消息Expected spy executeRules to have been called.

我在这里做错了什么?

angular typescript jasmine karma-runner
1个回答
0
投票
describe('EditcheckManagmentComponent', () => {
      let component: EditcheckManagmentComponent;
      let fixture: ComponentFixture<EditcheckManagmentComponent>;
      let debugElement: DebugElement;
      let mockEditChkManagementService: any;
      beforeEach(async(() => {
        mockEditChkManagementService = jasmine.createSpyObj('editCheckSVC', ['executeRules']);
        // can maybe create a variable BehaviorSubject so you can call next on it and send new values.
        mockEditChkManagemenetService.triggerRuleExecutionService = new BehaviorSubject({
         formName: '',
         schedule: '',
         subSchedule: '',
         version: '',
         fiscalYear: '2018',
         accountingPeriod: '6'
        });
        TestBed.configureTestingModule({
          declarations: [EditcheckManagmentComponent],
          schemas: [NO_ERRORS_SCHEMA],
          providers: [{ provide: EditCheckManagementService, useValue: mockEditChkManagemenetService}],
          imports: [HttpClientTestingModule] // bring in the testing HTTP, not actual implementation
        })
          .compileComponents();
      }));

      beforeEach(() => {

        fixture = TestBed.createComponent(EditcheckManagmentComponent);
        component = fixture.componentInstance;
        debugElement = fixture.debugElement;
        // make mockEditChkManagementService return an empty array
        mockEditChkManagementService.executeRules.and.returnValue(of([])); 
        // this first fixture.detectChanges() will call ngOnInit, no need to do it manually
        fixture.detectChanges();
      });


    it('should call rule execution API', () => {
       expect(mockEditChkManagementService.executeRules).toHaveBeenCalledWith({
         formName: '',
         schedule: '',
         subSchedule: '',
         version: '',
         fiscalYear: '2018',
         accountingPeriod: '6'
        });
      });
    });

尝试一下。要进行更多测试并进行简单设置,可能会很困难。您将不得不利用更多的describebeforeEach

关于您所拥有的,我不确定是哪里错。

© www.soinside.com 2019 - 2024. All rights reserved.