angular - SpyOn 调用方法的实际实现

标签 angular unit-testing karma-jasmine

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

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

我对这两种方法使用了 spy ,分别返回了数字 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.

需要帮助。!

谢谢

最佳答案

您需要在 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();
  });

关于angular - SpyOn 调用方法的实际实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272195/

相关文章:

javascript - 如何在不使用 ng serve 的情况下为 Angular 5 应用程序提供服务?

python - 实现从 Angular 到 Flask 的文件上传

javascript - 向 Angular 2 中的子组件发送搜索输入值

AndroidJUnitRunner 给出 'java.lang.ClassNotFoundException' 的 Android 概率

c# - 这还是Mock上的验证测试吗?

angular - 如何窥探可观察的属性(property)

typescript - 我的 Angular 2 应用程序需要很长时间才能为初次使用的用户加载,我需要帮助来加快速度

c# - 使用 PostSharp 进行正确的单元测试

Angular Jasmine 测试用订阅填充数组

angularjs - 当指令是属性时单元测试失败