问题描述
我正在测试的代码非常简单:如果条件被验证,它将调用一个方法.如果没有,它将调用第一个方法中包含的另一个方法作为属性.
The code I'm testing is fairly simple: it invokes a method in case a condition is verified. If not, it invokes another method contained within the first one as an attribute.
app.js:
function test (fn, isActivated) {
if (isActivated) {
return fn('foo')
}
return fn.subFn('bar')
}
var fn = function (p) { return p }
fn.subFn = function (p) { return 'sub-' + p }
var resFn = test(fn, true)
var resSubFn = test(fn, false)
document.write(resFn) // shows 'foo' as expected
document.write(resSubFn) // shows 'bar' as expected
我对每种方法都设置了一个间谍,但是当包含的方法subFn
上的间谍起作用时,fn
方法上的间谍似乎不起作用.参见下文:
I've set a spy on each method but the spy on the fn
method does not seem to work while the spy on the contained method subFn
works. See below:
app.test.js:
'use strict'
const chai = require('chai')
const sinon = require('sinon')
const trigger = require('../app').trigger
chai.should()
describe('test app', function () {
before(function () {
this.fn = function () {}
this.fn.subFn = function () {}
this.subFnSpy = sinon.spy(this.fn, 'subFn')
this.fnSpy = sinon.spy(this.fn)
})
describe('isActivated is true', function () {
before(function () {
trigger(this.fn, true)
})
it('should invoke fn', function () {
this.fnSpy.callCount.should.equal(1) // return false because callCount = 0
})
})
describe('isActivated is false', function () {
before(function () {
trigger(this.fn, false)
})
it('should invoke subFn', function () {
this.subFnSpy.callCount.should.equal(1) // return false because callCount = 0
})
})
})
闻到fn
函数的间谍有什么问题,我尝试了两种单独的方法.在这种情况下,两个间谍都将失败:
Smelling something wrong with the spy on the fn
function, I've tried with two separate methods. Both spies fail in this case:
app.js:
exports.trigger = function (fn, subFn, isActivated) {
if (isActivated) {
return fn('fn')
}
return subFn('bar')
}
app.test.js
'use strict'
const chai = require('chai')
const sinon = require('sinon')
const trigger = require('../app').trigger
chai.should()
describe('test app', function () {
before(function () {
this.fn = function () {}
this.subFn = function () {}
this.fnSpy = sinon.spy(this.fn)
this.subFnSpy = sinon.spy(this.subFn)
})
beforeEach(function () {
this.fnSpy.reset()
this.subFnSpy.reset()
})
describe('isActivated is true', function () {
before(function () {
trigger(this.fn, this.subFn, true)
})
it('should invoke fn if isActivated is true', function () {
this.fnSpy.callCount.should.equal(1) // return false
})
})
describe('isActivated is false', function () {
before(function () {
trigger(this.fn, this.subFn, false)
})
it('should invoke subFn if isActivated is true', function () {
this.subFnSpy.callCount.should.equal(1) // return false
})
})
})
关于我在做什么错的任何建议吗?
Any suggestion of what I'm doing wrong?
推荐答案
我没有找到确切的解决方案,但一种解决方法与之非常接近.因此,问题似乎在于用sinon.spy
处理this.fn
的方式,而不是这样做:
I did not find the exact solution but a workaround quite close from one. So the problem seems to lie with the way this.fn
is handled withtin sinon.spy
, thus instead of doing:
this.fnSpy = sinon.spy(this.fn)
this.subFnSpy = sinon.spy(this.subFn)
我们执行以下操作:
this.fnSpy = sinon.spy(this, 'fn')
this.subFnSpy = sinon.spy(this.fn, 'subFn')
我使用this
来存储fn
和subFn
的事实使这种情况减轻了.
The is easd by the fact that I use this
to store fn
and subFn
.
这篇关于不会调用方法上的Sinon.spy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!