【问题标题】:SpyOn calling actual implementation of methodSpyOn 调用方法的实际实现
【发布时间】:2018-05-10 11:43:33
【问题描述】:

我正在尝试为 Angular 应用程序编写单元测试测试用例,并且我正在使用 SpyOn() 方法来监视服务方法。

我正在测试一个服务,该服务有一个名为 getCurrentBoardTimeIdByCurrentTime() 的方法,该方法在内部调用另一个名为
utilService.getHour()utilService.getWeekday() 的服务方法

我在这两种方法上使用了间谍,并分别返回了数字25,之后getCurrentBoardTimeIdByCurrentTime() 必须返回7

现在,当我调用服务方法getCurrentBoardTimeIdByCurrentTime() 时,不会使用来自 spy 的返回值,而是调用实际函数本身导致测试失败。

BoardSharedService.spec.ts

describe('BoardSharedService', () => {
  let service: BoardSharedService;
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        BoardSharedService,
        UtilService
      ]
    });
  });

  it('should fetch data', () => {
    service = TestBed.get(BoardSharedService);
    const getHourSpy = jasmine.createSpyObj('UtilService', ['getHour']);
    const getWeekDaySpy = jasmine.createSpyObj('UtilService', ['getWeekDay']);

    getHourSpy.getHour.and.returnValue(2);
    getWeekDaySpy.getWeekDay.and.returnValue(5);

    expect(service.getCurrentBoardTimeIdByCurrentTime()).toBe(7);
    expect(service.getCurrentBoardTimeIdByCurrentTime).toHaveBeenCalled();
  });

});

和 boardSharedService.ts

@Injectable()
export class BoardSharedService {

  constructor(private utilService: UtilService) { }

  getCurrentBoardTimeIdByCurrentTime() {
    const currentHour = this.utilService.getHour();
    const currentDay = this.utilService.getWeekDay();
    if (currentHour < 6 || currentHour > 17) {
      // PM
      if (currentDay === Day.Friday) {
        return 7; // Friday PM
      } 
    }
  }
}

我收到以下错误

BoardSharedService should fetch data
Expected 1 to be 7.
Error: Expected 1 to be 7.

需要帮助!

谢谢

【问题讨论】:

    标签: angular unit-testing karma-jasmine


    【解决方案1】:

    您需要在UtilService 的提供者中提供jasmine spyObj

    然后你可以.and.returnValue(some_value)UtilService的方法。

    providers: [
            BoardSharedService,
            {provide : UtilService, useValue: jasmine.createSpyObj('UtilService', ['getHour', 'getWeekDay']);
          ]
    

    在规范中你可以做这样的事情

      it('should fetch data', () => {
        // UPDATE: You are doinf expect(service.getCurrentBoardTimeIdByCurrentTime).toHaveBeenCalled();
        // And you have not spy'd on service.getCurrentBoardTimeIdByCurrentTime method, it will throw error.
        jasmine.spyOn(service, 'getCurrentBoardTimeIdByCurrentTime').and.callThrough();
        service = TestBed.get(BoardSharedService);
    
        let utilService= TestBed.get(UtilService);
    
        utilService.getHour.and.returnValue(2);
        utilService.getWeekDay.and.returnValue(5);
    
        expect(service.getCurrentBoardTimeIdByCurrentTime()).toBe(7);
        expect(service.getCurrentBoardTimeIdByCurrentTime).toHaveBeenCalled();
      });
    

    【讨论】:

    • 它工作了一些,但现在我得到错误: : Expected a spy, but got Function。用法:expect().toHaveBeenCalled()
    • @KedarUdupa,我已经更新了答案,您需要执行jasmine.spyOn(service, 'getCurrentBoardTimeIdByCurrentTime').and.callThrough();,因为您希望该方法已被调用。
    • 这工作..谢谢。 @Basavaraj Bhusani
    猜你喜欢
    • 2020-08-11
    • 2019-01-15
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多