在Angular 6茉莉花中获取错误“预期的间谍<functionName>已被调用”

时间:2019-06-15 08:11:21

标签: angular unit-testing karma-jasmine

我在组件中有一个方法“ onEdit”。在该方法中,我将服务方法称为“ getAttributeById”,我在“ getAttributeById”服务方法中进行了api调用并返回一个可观察值,并且为此编写了一个单元测试用例,但我遇到了错误

  

“预期的间谍getAttributeById已被调用。”

onEdit(row) {
    if (this.role != 'Admin' && this.role != 'Owner') {
      return;
    }
    this.loadingData = true;
    this._service.getAttributeById(row['id']).subscribe((data) => {
      this.editing = true;
      this.newAttribute = { ...data };
      data.isEdit = true;

      this.loadingData = false;
      this._service.setData({attribute: this.newAttribute});
      this.router.navigate(['attributes-edit']);
    })   }

规格文件

describe('MetaDataComponent', () => {
  let component: MetaDataComponent;
  let fixture: ComponentFixture<MetaDataComponent>;
  beforeEach(() => {
    const metaDataServiceStub = {
      getVariables: query1 => ({ subscribe: () => ({}) }),
      getAttributeById: arg1 => ({ subscribe: () => ({}) }),
      setData: object1 => ({}),
      deleteAttribute: arg1 => ({ subscribe: () => ({}) })
    };
    const routerStub = { navigate: array1 => ({}) };
    TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [MetaDataComponent],
      providers: [
        { provide: MetaDataService, useValue: metaDataServiceStub },
        { provide: Router, useValue: routerStub }
      ]
    });
    spyOn(MetaDataComponent.prototype, 'getAttributes');
    fixture = TestBed.createComponent(MetaDataComponent);
    component = fixture.componentInstance;
  });
it('onEdit', () => {
      const payload = {id: '11111'};
      const metaDataServiceStub: MetaDataService = fixture.debugElement.injector.get(MetaDataService);
      const editSpy = spyOn(metaDataServiceStub, 'getAttributeById').and.returnValue(of({}));
      expect(component.onEdit(payload)).toBeUndefined();
      expect(editSpy).toHaveBeenCalled();
    });

我遇到错误了

Expected spy getAttributeById to have been called.
Error: Expected spy getAttributeById to have been called.
    at stack (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2455:17)
    at buildExpectationResult (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2425:14)
    at Spec.expectationResultFactory (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:901:18)
    at Spec.addExpectationResult (http://localhost:9876/absoluteC:/Users/U1192079/Projects/r2d2-ui/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:524:34)
    at Ex

1 个答案:

答案 0 :(得分:1)

我不是该测试框架的专家,但我注意到这条线

const editSpy = spyOn(metaDataServiceStub,'getAttributeById')。and.returnValue(of({}));

我不知道部分“ .and.refurnValue”是否属于匹配项。如果是这样,我认为要将方法getAttributeById视为“已调用”,您说它应该返回一个空对象。...我认为它永远不会做,因为您已将其设置为返回包含预订的对象()方法被调用时。