不调用方法上的 Sinon.spy
Sinon.spy on a method is not invoked
我正在测试的代码相当简单:它在验证条件时调用一个方法。如果不是,它将调用第一个方法中包含的另一个方法作为属性。
应用.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
我已经在每种方法上设置了一个间谍,但是fn
方法上的间谍似乎不起作用,而对所包含方法的间谍subFn
工作。见下文:
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
功能有问题,我尝试了两种不同的方法。在这种情况下,两个间谍都失败了:
应用.js:
exports.trigger = function (fn, subFn, isActivated) {
if (isActivated) {
return fn('fn')
}
return subFn('bar')
}
应用测试.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
})
})
})
对我做错了什么有什么建议吗?
我没有
找到确切的解决方案,但有一个非常接近的解决方法。所以问题似乎在于this.fn
在sinon.spy
的情况下处理的方式,因此而不是这样做:
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
这一事实很容易。
相关文章:
- 未调用 Sinon 存根
- node.js测试事件是否是在不使用超时的情况下使用sinon.js发出的
- Sinon Spy不使用Javascript调用或应用程序
- Stubbing Backbone类构造函数与Jasmin和Sinon
- 什么'与Jasmine相比,使用Sinon.js的优势是什么;内置于间谍
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- Sinon窥探功能表达
- Sinon Mocha节点错误处理
- sinon.js验证mock方法's带回调的参数
- 使用sinon.js创建一个“;“间谍对象”;使用基于真实构造函数/原型的间谍方法
- 如何在测试异步运行时使用 Sinon 沙箱
- stub.callsArg(index) from Sinon.JS 是做什么的
- Sinon:存根和整个对象,并将其替换为新对象
- Stubbing async.waterfall with Sinon.JS
- 测试Express.js res.render在承诺与Mocha&Sinon spy
- 不调用方法上的 Sinon.spy
- 如果间接调用spied方法,则不会调用Sinon Spy
- sinon-spy在异步回调中不包装方法
- Mocha JS测试——一种对sinon spy进行回调的方法
- Sinon spy for函数不起作用