javascript - 如何测试通过的功能?
问题描述
doc 是 pdfkit 文档的一个实例...
import PDFDocument from 'pdfkit'
const doc = new PDFDocument()
...它被传递给我的函数:
export const outputTitle = (doc, title) => {
if (!title) return null
doc
.fontSize(15)
.font('Helvetica-Bold')
.text(title, 380, 160)
}
现在我需要使用 jest 为这个函数编写单元测试。
describe('outputTitle()', () => {
const doc = jest.fn()
test('should return null if parameter title is missing', () => {
// SETUP
const title = undefined
// EXECUTE
const result = outputTitle(doc, title)
// VERIFY
expect(result).toBeNull()
})
test('should call doc()', () => {
// ???
})
})
但是我如何测试第二部分,即传递标题值的情况?我认为我的嘲笑doc
是错误的。
解决方案
describe('outputTitle()', () => {
const textSpy = jest.spyOn(doc, 'text');
test('should call doc with title', () => {
outputTitle(doc, 'some title');
expect(textSpy).toBeCalledWith('some title');
});
})
推荐阅读
- unity3d - 按钮被另一个按钮遮挡,无法按下
- python - PIL 中有没有办法将图像保存为 URL?
- javascript - 如何使用带有 js/jquery 的电报机器人以 html 形式发送照片?
- python - 用一个白色像素替换不规则形状
- javascript - jQuery:根据数量/价格计算最终总数
- capl - 使用 dll 文件 CANOE CAPL 生成安全访问密钥
- docker - 如何在 vscode 中定义文件名模式以检查语言
- python - 如何摆脱此错误:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (8000,)
- javascript - 在“React-Query”中所有并行查询成功后如何执行操作
- amazon-ec2 - AWS:我在 AWS 上部署了我的微服务,它工作正常,但服务类无法从文件中读取数据