角度UNIT TEST未能在rxjs的.pipe()内的.map()内传递的回调方法

时间:2019-03-21 06:58:42

标签: angular unit-testing jasmine karma-jasmine angular-testing

.ts文件代码

@Component({
  selector: 'app-nav-breadcrumbs',
  templateUrl: './nav-breadcrumbs.component.html',
  styleUrls: ['./nav-breadcrumbs.component.scss']
})
export class NavBreadcrumbsComponent implements OnInit, OnDestroy {
  private subscriptions = new Subscription();


  constructor(
    private _router: Router
  ) {
  }

  ngOnInit() {
    this.subscriptions.add(
      this._router.events.pipe(
        filter(event => event instanceof NavigationEnd),
        distinctUntilChanged(),
        map(event => this.myMethod())
      ).subscribe({
        next: (res) => {
          // ... 
        },
        error: (e) => {
          console.warn(e);
        }
      })
    );
  }
  ngOnDestroy() {
    this.subscriptions.unsubscribe();
  }

  myMethod() {

    // some logic...

  }

}

我的规格文件

describe('NavBreadcrumbsComponent', () => {
  let component: NavBreadcrumbsComponent;
  let fixture: ComponentFixture<NavBreadcrumbsComponent>;
  beforeEach(() => {
    const routerStub = {
      events: { pipe: () => ({ subscribe: () => ({}) }) },
      navigate: () => ({})
    };
    TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [NavBreadcrumbsComponent],
      providers: [
        { provide: Router, useValue: routerStub }
      ]
    });
    fixture = TestBed.createComponent(NavBreadcrumbsComponent);
    component = fixture.componentInstance;
  });
  it('can load instance', () => {
    expect(component).toBeTruthy();
  });
  describe('ngOnInit', () => {
    it('makes expected calls', () => {
      spyOn(component, 'myMethod');
      component.ngOnInit();
      expect(component.myMethod).toHaveBeenCalled();
    });
  });
});

当前低于错误

NavBreadcrumbsComponent ngOnInit makes expected calls
Expected spy myMethod to have been called.
Error: Expected spy myMethod to have been called.
    at stack (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2455:17)
    at buildExpectationResult (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2425:14)
    at Spec.expectationResultFactory (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:901:18)
    at Spec.addExpectationResult (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:524:34)
    at Expectation.addExpectationResult (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:845:21)
    at Expectation.toHaveBeenCalled (http://localhost:9876/absoluteD:/projects/work/CHI/CHII2I_landing/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2369:12)
    at UserContext.eval (webpack:///./src/app/core/components/nav-breadcrumbs/nav-breadcrumbs.component.spec.ts?:45:47)
    at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone.js?:390:26)
    at ProxyZoneSpec.onInvoke (webpack:///./node_modules/zone.js/dist/zone-testing.js?:288:39)
    at ZoneDelegate.invoke (webpack:///./node_modules/zone.js/dist/zone.js?:389:52)

当前,我无法监视并测试toHaveBeenCalled的myMethod()方法。由于在.map()函数内部被调用,而在.pipe()函数内部被调用,因此我无法从外部捕获方法调用。请建议如何监视和测试myMethod方法(只是期望(myMethod).toHaveBeenCalled())

0 个答案:

没有答案