【问题标题】:Get an Error "Expected spy <functionName> to have been called" in angular 6 jasmine在 Angular 6 jasmine 中获取错误“预期的间谍 <functionName> 已被调用”
【发布时间】:2019-06-15 16:50:50
【问题描述】:

我在组件中有一个方法“onEdit”。在那个方法中,我调用了服务方法“getAttributeById”,我在“getAttributeById”服务方法中进行了一个 api 调用并返回了一个 observable,我正在为它编写一个单元测试用例,但我得到了一个错误

"预期的间谍 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

【问题讨论】:

    标签: angular unit-testing karma-jasmine


    【解决方案1】:

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

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

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

    【讨论】:

    • 我们可以这样做也是因为我没有写任何关于subscribe()响应的期望,我只是检查它是否被调用了?
    • 我的意思是,是否可以更改:“const editSpy = spyOn(metaDataServiceStub, 'getAttributeById').and.returnValue(of({}));”进入“const editSpy = spyOn(metaDataServiceStub, 'getAttributeById');”这能解决您的测试问题吗?
    • 其他几件事要检查:1)您有 2 个包含模拟服务的变量。它们是否指向完全相同的对象实例? 2)在您检查的方法之前有一个 if 语句(检查一些角色)。你确定你的方法被调用了吗?
    • ... 和 3) 仔细检查您的事实。你已经尝试了一些你说没有用的东西。你确定他们没有因为完全相同的原因工作吗? (或者它是否解决了您的问题,但随后您在代码中遇到了下一个问题,并且看到失败的测试让您认为这是出于相同的原因?)
    • 是的丹尼斯,你带我去正确的方向,你的第二点是真的,有一个 if 语句不允许执行其中的代码。谢谢哥们。我正在投票您的答案,但不将其标记为答案,
    猜你喜欢
    • 2019-02-05
    • 2018-04-08
    • 2023-03-24
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多