- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在练习测试先行的开发,我想确保类中的方法总是在 warn 级别调用我的记录器并显示一条消息。我的类(class)定义如下:
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
但这似乎也不起作用。 😕 欢迎提出任何建议!
最佳答案
模拟的事情是你需要提供模拟,对我来说最简单的方法是通过 mock factory .但是我也会推荐一些重构:
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
})
})
关于unit-testing - 如何模拟对 logger.warn 的调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275304/
作为一个新人中的菜鸡,最近在看一个项目的代码其中有这么一段看得我很是费解 public class AssembleMsgService { private static Logger logger
我已更改 Gradle 构建脚本以使用 Spring 2.0.1 而不是 1.5.9。当我运行 gradle build 时,出现错误:找不到符号 Logger logger = Logger.get
这是我的示例代码: public class Logs { private static Logs logHandler; public static Logs handler() {
我目前正在使用 Java 日志记录。我有一个记录器 cwh.A 和 cwh.B。是否可以通过 Java 从记录器 cwh 获取这些记录器,而不知道它们的名称? 最佳答案 可以通过日志管理器获取所有记录
在我的应用程序中,有一次我将 java.util.logging 的根 Logger 及其所有处理程序设置为 Level.FINEST,如下所示: Logger.getLogger("").setLe
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
我刚刚将 Java 插件升级到 1.7.0_25,这导致我几年前编写的一个小程序出现 fatal error (此后一直在维护)。 在小程序的 init() 方法的早期,我调用了 logger = L
logger.debug 和 logger.info 有什么区别? logger.debug什么时候打印? 最佳答案 我建议你看看这篇名为 "Short Introduction to log4j"
Fixed I tried reinstalling the plugin but that didn't work so i completely cleared my inteljii idea
我定义了一个记录器实例如下: private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); 我有一个要记录的
我正在 Windows 下使用 Python 3.4.2。就我而言, import logging logger = logging.getLogger('logger') logger.setLev
我目前正在使用 C# 开发一个新的 Prism 项目,并想在我的 Bootstrapper 类中创建一个 NLog Logger。不幸的是,无论我做什么,它都拒绝构建,并告诉我... 'Logger.
我已经有几年没有使用 Log4J 了。我现在正在使用 org.jboss.logging 包在 JBoss 5 下开发一个应用程序。当我以前这样做时,将 logger.info() 消息包含在日志记录
ActiveRecord::Base.logger = Logger.new(STDOUT) 的 redis 等价物是什么?我想看看redis在做什么。 最佳答案 $redis.client.logg
我正在尝试使用 log4js 创建日志文件。完成所有配置后运行 Node app.js 时,我收到以下错误。 错误: TypeError: logger.setLevel is not a funct
我在一个拥有大约 50 多名开发人员的 IT 部门工作。它曾经有大约 100 多名开发人员,但由于经济衰退而被削减。 当我们的部门规模扩大时,我们雄心勃勃地努力建立一个特殊的架构小组。 这个小组决定做
我正在尝试找出跨多个模块使用 python 日志记录的最佳实践。我在这里看到:http://docs.python.org/2/howto/logging#logging-from-multiple-
我想将 Logger 注入(inject)到在 WildFly 10 上运行的 Maven 动态 Web 项目中的 bean 中。 我在 POM 中添加了依赖项。 WildFly 中提供了范围:
Java 9 引入了一个新的记录器,即 java.lang.System.Logger但我们总是有java.util.logging.Logger . 此记录器有什么新功能以及它有何改进? 最佳答案
我在 eclipse Mars 中配置了我的环境,但是 log4j 出现错误。我使用 spring 4 和 Maven 作为依赖管理器。部分代码: Eclipse 在这一行显示了一个错误,等于后的第二
我是一名优秀的程序员,十分优秀!