茉莉花callFake不被使用;实函数仍称为

时间:2019-10-18 17:34:23

标签: angular jasmine

我正在尝试使用spyOn和callFake模拟函数调用,但仍在调用真实函数,而不是伪函数。

组件代码:

async getOrderProfile() {
    let ordersSuccess = false;
    ...
    try {
      const ordersResponse = await this.ordersService.getOrderProfile(params);      
      ordersSuccess = this.validateResponse(ordersResponse);
       ...
    } catch (error) {
       ...
    }

  }

规格:

describe('OrdersComponent', () => {
  let component: OrdersComponent;
  let fixture: ComponentFixture<OrdersComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [OrdersComponent],
      imports: [],
      providers: []
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(OrdersComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should call getOrdersNoteData', () => {
    ...
    spyOn(component, 'validateResponse').and.callFake(() => {
      console.log('faked method');
      return true;
    });
    component.getOrderProfile();
    ...
  });
});

我没有看到我的测试语句记录到控制台,并且真实函数抛出错误,因为它没有所有预期的数据。我不确定在这方面做错什么。

我也没有运气就使用过spyOn returnValue:

spyOn(component, 'validateResponse').and.returnValue(true)

0 个答案:

没有答案