gpt4 book ai didi

javascript - 如何监视 ES6 中命名导出的子函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:26 25 4
gpt4 key购买 nike

我想监视作为命名导出导出的子函数,但是,我们似乎无法监视它。

假设我在 operations.js 中有两个名为 addmultiply 的函数,并将它们导出为命名导出:

const add = (a, b) => {
return a + b
}

const multiply = (a, b) => {
let result = 0

for (let i = 0; i < b; i++) {
result = add(a, result)
}
return result
}

export { add, multiply }

并且测试文件使用 sinon-chai 来尝试监视 add 函数:

import chai, { expect } from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
import * as operations from './operations'

chai.use(sinonChai)

describe('multiply', () => {
it('should call add correctly', () => {
sinon.spy(operations, 'add')

operations.multiply(10, 5)
expect(operations.add).to.have.been.callCount(5)

operations.add.restore()
})
})

结果是

AssertionError: expected add to have been called exactly 5 times, but it was called 0 times

但是,如果我像下面这样直接调用 operations.add,它会通过测试:

describe('multiply', () => {
it('should call add correctly', () => {
sinon.spy(operations, 'add')

operations.add(0, 5)
operations.add(0, 5)
operations.add(0, 5)
operations.add(0, 5)
operations.add(0, 5)
expect(operations.add).to.have.been.callCount(5)

operations.add.restore()
})
})

似乎 sinon-spy 为 operations.add 创建了一个新引用,但是 multiply 函数仍然使用已经绑定(bind)的旧引用。

如果这些函数被命名为 exports,那么监视此 multiply 函数的 add 函数的正确方法是什么?

一般来说,如何监视被测试的父函数的子函数,它们都被命名为 exports?

[更新]

multiply 函数只是一个例子。我的主要观点是测试父函数是否调用子函数。但是,我不希望该测试依赖于子功能的实现。所以,我只想监视子函数是否被调用。您可以想象 multiply 函数是一个 registerMember 函数,而 add 函数是一个 sendEmail 函数。 (这两个函数都被命名为导出。)

最佳答案

我有一个解决我自己的问题的方法。

目前,multiply() 函数与add() 函数紧密耦合。这使得测试变得困难,尤其是当导出的函数获得新引用时。

因此,为了监视子函数调用,我们可以将子函数传递给父函数。是的,这就是依赖注入(inject)

因此,在 operations.js 中,我们将 addFn 注入(inject)到 multiply() 中,并按如下方式使用它:

const add = (a, b) => {
return a + b
}

const multiply = (a, b, addFn) => {
let result = 0

for (let i = 0; i < b; i++) {
result = addFn(a, result)
}
return result
}

export { add, multiply }

然后,在测试中,我们可以像这样监视 add() 函数:

describe('multiply', () => {
it('should call add correctly', () => {
sinon.spy(operations, 'add')

operations.multiply(10, 5, operations.add)
expect(operations.add).to.have.been.callCount(5)

operations.add.restore()
})
})

现在,它的作用是测试子函数是否被正确调用。

(注意:缺点是我们需要更改multiply()函数)

关于javascript - 如何监视 ES6 中命名导出的子函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com