【问题标题】:How do I mock the imports my tested class makes with jest?如何模拟我的测试类用玩笑所做的导入?
【发布时间】:2019-10-26 23:53:30
【问题描述】:

我正在尝试为我的数据库爬虫程序设置测试,但我无法替换我正在测试的类方法导入的内容。

为了不写太多代码,我只列出问题的一般形式。在我的测试功能中,我有:

describe("test",()=>{
  let result1;

  beforeAll(async ()=>{
    await createConnection();
  })

  afterAll(async ()=>{
    getConnection().close();
  })

  test("setup test",async () => {
    result1 = await WeatherController.startForecastAPI();
    expect(result1.status).toBe(Status.SUCCESS);
  })
})

WeatherController.ts 文件(...其中代码被取出):

...
import AccessTokenService from '../services/AccessTokenService';

export default class WeatherController{
    ...
    static async startForecastAPI(){
           ...
           const accessToken = AccessTokenService.getAccessToken();//get and validate token 
           ...        
    }
}

在 WeatherController 类中,startForecastAPI 被定义为静态异步方法。该类导入了多个其他类,其中包括用于获取有效访问令牌的 AccessTokenService 类。 AccessTokenService.getAccessToken() 应该返回一个对象,该对象具有通过 http 请求获得的多个属性。

我想模拟调用 AccessTokenService 的结果,但我没有直接在我的测试函数中调用它,我正在调用 Wea​​therController 而 WeatherController 正在调用 AccessTokenService。如何在测试时替换 WeatherController 调用的内容但不触及 WeatherController 代码?我已经尝试过浏览 jest 文档,但我对这一切都很陌生,而且它们令人困惑。我也不完全清楚范围是如何在这里工作的(我尝试在测试代码中定义一个函数并在测试函数中调用它,但它超出了范围)。

测试函数中的 await WeatherController.startForecastAPI() 调用返回未定义,但是当我将 accessToken 硬编码为有效对象时,代码工作正常,我只是找不到将该对象注入代码的方法测试函数。

【问题讨论】:

    标签: javascript testing mocking jestjs


    【解决方案1】:

    假设 AccessTokenService.getAccessToken 返回一个 promise 或者是一个异步函数,那么你可以使用 jest.spyOn(...).mockResolvedValue() 来阻止调用服务器

    describe("test",()=>{
      let result1;
    
      beforeAll(async ()=>{
        await createConnection();
      })
    
      afterAll(async ()=>{
        getConnection().close();
      })
    
      test("setup test",async () => {
        const expectedResultFromGetToken = {property: 'property 1'};
        const getTokenSpy = jest.spyOn(AccessTokenService, 'getAccessToken')
                                .mockResolvedValue(expectedResultFromGetToken)
        result1 = await WeatherController.startForecastAPI();
        expect(result1.status).toBe(Status.SUCCESS);
        expect(getTokenSpy).toHaveBeenCalled()
      })
    })
    

    如果 AccessTokenService.getAccessToken 不是异步函数,那么您必须使用 jest.spyOn(...).mockReturnValue()

    【讨论】:

    • 我在帖子中添加了更多信息。
    • 您需要做的是,在您的测试用例中,您可以通过 jest.spyOn(AccessTokenService, 'getAccessToken').mockResolvedValue({propert1: 'I'm a模拟对象'})
    • 现在编辑我的答案
    【解决方案2】:

    如果在你的班级里你有

    const AccessToken = require('access-token');
    

    你可以用它来模拟它

    jest.mock('access-token', () => {
       function getToken() {
         return 'fakeToken'
       }
    );
    
    const WeatherController = require('weather-controller');
    
    describe("test",()=>{
      let result1;
    
      beforeAll(async ()=>{
        await createConnection();
      })
    
      afterAll(async ()=>{
        getConnection().close();
      })
    
      test("setup test",async () => {
        result1 = await WeatherController.startForecastAPI();
        expect(result1.status).toBe(Status.SUCCESS);
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2020-05-27
      • 2021-06-16
      • 2022-01-21
      • 2020-12-14
      • 1970-01-01
      • 2021-02-10
      • 2020-09-23
      • 2018-09-18
      相关资源
      最近更新 更多