如何检查在 Sinon 中是否使用正确的属性调用了 class 构造函数?
How can I check that a class constructor is called with proper attributes in Sinon?
我正在测试从外部库实例化对象的代码。为了使其可测试,我决定注入依赖项:
归结为:
const decorator = function (obj, _extLib) {
var ExtLib = _extLib || require('extlib')
config = determineConfig(obj) //This is the part that needs testing.
var el = new ExtLib(obj.name, config)
return {
status: el.pay({ amt: "one million", to: "minime" })
bar: obj.bar
}
}
在我的测试中,我需要确定外部库是用正确的 config
实例化的。我对这个外部库是否有效(有效)或调用它是否给出结果不感兴趣。为了这个例子,我们假设在实例化时,它调用一个慢银行 API 然后锁定数百万美元:我们希望它存根、嘲笑和监视。
在我的测试中:
it('instantiates extLib with proper bank_acct', (done) => {
class FakeExtLib {
constructor(config) {
this.acct = config.bank_acct
}
this.payMillions = function() { return }
}
var spy = sandbox.spy(FakeExtLib)
decorator({}, spy) // or, maybe decorator({}, FakeExtLib)?
sinon.assert.calledWithNew(spy, { bank_acct: "1337" })
done()
})
请注意测试是否el.pay()
被调用,工作正常,使用间谍,在 sinon 中。 new
的实例化似乎无法测试。
为了调查,让我们让它更简单,内联测试所有内容,避开被测对象,decorator
完全发挥作用:
it('instantiates inline ExtLib with proper bank_acct', (done) => {
class ExtLib {
constructor(config) {
this.acct = config.bank_acct
}
}
var spy = sandbox.spy(ExtLib)
el = new ExtLib({ bank_acct: "1337" })
expect(el.acct).to.equal("1337")
sinon.assert.calledWithNew(spy, { bank_acct: "1337" })
done()
})
expect
部分通过。所以显然它都被正确调用了。但是 sinon.assert
失败了。仍然。为什么?
如何检查 class 构造函数在 Sinon 中是否使用正确的属性调用?” calledWithNew
可以这样使用吗?我应该监视另一个函数,例如 ExtLib.prototype.constructor
而不是?如果是,怎么做?
你真的很接近。
对于最简单的示例,您只需要使用 spy
而不是 ExtLib
创建 el
:
it('instantiates inline ExtLib with proper bank_acct', (done) => {
class ExtLib {
constructor(config) {
this.acct = config.bank_acct
}
}
var spy = sandbox.spy(ExtLib)
var el = new spy({ bank_acct: "1337" }) // use the spy as the constructor
expect(el.acct).to.equal("1337") // SUCCESS
sinon.assert.calledWithNew(spy) // SUCCESS
sinon.assert.calledWithExactly(spy, { bank_acct: "1337" }) // SUCCESS
done()
})
(请注意,我修改了测试以使用 calledWithExactly
来检查参数,因为 calledWithNew
在 v7.2.2 中似乎无法正确检查参数)
我正在测试从外部库实例化对象的代码。为了使其可测试,我决定注入依赖项:
归结为:
const decorator = function (obj, _extLib) {
var ExtLib = _extLib || require('extlib')
config = determineConfig(obj) //This is the part that needs testing.
var el = new ExtLib(obj.name, config)
return {
status: el.pay({ amt: "one million", to: "minime" })
bar: obj.bar
}
}
在我的测试中,我需要确定外部库是用正确的 config
实例化的。我对这个外部库是否有效(有效)或调用它是否给出结果不感兴趣。为了这个例子,我们假设在实例化时,它调用一个慢银行 API 然后锁定数百万美元:我们希望它存根、嘲笑和监视。
在我的测试中:
it('instantiates extLib with proper bank_acct', (done) => {
class FakeExtLib {
constructor(config) {
this.acct = config.bank_acct
}
this.payMillions = function() { return }
}
var spy = sandbox.spy(FakeExtLib)
decorator({}, spy) // or, maybe decorator({}, FakeExtLib)?
sinon.assert.calledWithNew(spy, { bank_acct: "1337" })
done()
})
请注意测试是否el.pay()
被调用,工作正常,使用间谍,在 sinon 中。 new
的实例化似乎无法测试。
为了调查,让我们让它更简单,内联测试所有内容,避开被测对象,decorator
完全发挥作用:
it('instantiates inline ExtLib with proper bank_acct', (done) => {
class ExtLib {
constructor(config) {
this.acct = config.bank_acct
}
}
var spy = sandbox.spy(ExtLib)
el = new ExtLib({ bank_acct: "1337" })
expect(el.acct).to.equal("1337")
sinon.assert.calledWithNew(spy, { bank_acct: "1337" })
done()
})
expect
部分通过。所以显然它都被正确调用了。但是 sinon.assert
失败了。仍然。为什么?
如何检查 class 构造函数在 Sinon 中是否使用正确的属性调用?” calledWithNew
可以这样使用吗?我应该监视另一个函数,例如 ExtLib.prototype.constructor
而不是?如果是,怎么做?
你真的很接近。
对于最简单的示例,您只需要使用 spy
而不是 ExtLib
创建 el
:
it('instantiates inline ExtLib with proper bank_acct', (done) => {
class ExtLib {
constructor(config) {
this.acct = config.bank_acct
}
}
var spy = sandbox.spy(ExtLib)
var el = new spy({ bank_acct: "1337" }) // use the spy as the constructor
expect(el.acct).to.equal("1337") // SUCCESS
sinon.assert.calledWithNew(spy) // SUCCESS
sinon.assert.calledWithExactly(spy, { bank_acct: "1337" }) // SUCCESS
done()
})
(请注意,我修改了测试以使用 calledWithExactly
来检查参数,因为 calledWithNew
在 v7.2.2 中似乎无法正确检查参数)