3

我正在练习测试优先开发,我想确保类中的方法总是在警告级别调用我的记录器并显示一条消息。我的班级是这样定义的:

import { log4js } from '../config/log4js-config'

export const logger = log4js.getLogger('myClass')

class MyClass {
  sum(numbers) {
    const reducer = (accumulator, currentValue) => accumulator + currentValue
    const retval = numbers.reduce(reducer))
    if (retval < 0) {
      logger.warn('The sum is less than zero!')
    }
    return retval
  }
}

const myClass = new MyClass()
export { myClass }

我的测试如下所示:

import { myClass, logger } from './MyClass'
import { log4js } from '../config/log4js-config'

jest.mock('log4js')

describe('MyClass', () => {

  it('logs a warn-level message if sum is negative', () => {
    logger.warn = jest.fn()
    logger._log = jest.fn()
    myClass.sum([0, -1])
    expect(logger.warn).toHaveBeenCalled() // <--- fails
    expect(logger._log).toHaveBeenCalled() // <--- fails
  })
})

我也尝试log4js.Logger._log在设置中进行模拟,但这似乎也不起作用。任何建议表示赞赏!

4

2 回答 2

3

模拟的事情是你需要提供模拟,对我来说最简单的方法是通过模拟工厂。但是,我还建议进行一些重构:

import { getLogger } from 'log4js'

export const logger = getLogger('myClass')
logger.level = 'debug'

// export the class itself to avoid memory leaks
export class MyClass {
  // would consider even export just the sum function
  sum(numbers) {
    const reducer = (accumulator, currentValue) => accumulator + currentValue
    const retval = numbers.reduce(reducer))
    if (retval < 0) {
      logger.warn('The sum is less than zero!')
    }
    return retval
  }
}

import log4js from 'log4js';
import { MyClass } from "./class";

jest.mock('log4js', () => {
    // using the mock factory we mimic the library.

    // this mock function is outside the mockImplementation 
    // because we want to check the same mock in every test,
    // not create a new one mock every log4js.getLogger()
    const warn = jest.fn()
    return {
        getLogger: jest.fn().mockImplementation(() => ({
            level: jest.fn(),
            warn,
        })),
    }
})

beforeEach(() => {
    // reset modules to avoid leaky scenarios
    jest.resetModules()
})

// this is just some good habits, if we rename the module
describe(MyClass, () => {

    it('logs a warn-level message if sum is negative', () => {
        const myClass = new MyClass()
        myClass.sum([0, -1])

        // now we can check the mocks
        expect(log4js.getLogger).toHaveBeenCalledTimes(1) // <--- passes
        // check exactly the number of calls to be extra sure
        expect(log4js.getLogger().warn).toHaveBeenCalledTimes(1) // <--- passes
    })

})
于 2019-08-09T00:49:27.523 回答
1

也许简单地监视记录器方法就可以了

import { myClass, logger } from './MyClass'

describe('MyClass', () => {

  it('logs a warn-level message if sum is negative', () => {
    const warnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {});
    const _logSpy = jest.spyOn(logger, '_log').mockImplementation(() => {});
    myClass.sum([0, -1])
    expect(warnSpy).toHaveBeenCalled()
    expect(_logSpy).toHaveBeenCalled()
  })
})
于 2019-08-15T21:23:06.113 回答