我试图为express中间件功能设置存根伪造,但它并没有被取代。
我正试图通过callsFake函数使用sinon stubbing,正如他们最新文档中建议的那样。
即使我需要模块并在导出时替换属性中的函数。我一直看到原来的函数行为在起作用。
我知道我应该在安装中间件函数之前尝试将函数存根,而这正是第一次导入expressapp
的时候。
这是我尝试存根的函数,定义为函数并导出为对象。它在脚本文件中定义,路径如api/middlewares/stripe/sign
。
const stripeHelper = require('../../../lib/stripe')
const logger = require('../../../lib/logger')
const verifySignature = (req, res, next) => {
var event
let eventName = req.url.replace('/', '')
try {
// Try adding the Event as `request.event`
event = stripeHelper.signatureCheck(
eventName,
req.body,
req.headers['stripe-signature']
)
} catch (e) {
// If `constructEvent` throws an error, respond with the message and return.
logger.error('Error while verifying webhook request signature', e.message, e)
return res.status(400).send('Webhook Error:' + e.message)
}
req.event = event
next()
}
module.exports.verifySignature = verifySignature
这是我的存根和测试挂钩设置:
const chai = require('chai')
const chaiHttp = require('chai-http')
const dirtyChai = require('dirty-chai')
const sinon = require('sinon')
const decache = require('decache')
const signatureMiddleware = require('../../../api/middlewares/stripe/signature')
const bp = require('body-parser')
let verifySignatureStub, rawStub
chai.should()
chai.use(dirtyChai)
chai.use(chaiHttp)
const API_BASE = '/api/subscriptions'
const planId = 'NYA-RUST-MONTHLY'
const utils = require('../../utils')
const {
hooks: {createSubscription, emitPaymentSucceeded},
stripe: {generateEventFromMock}
} = utils
let testUser, testToken, testSubscription, server
describe.only('Subscriptions renewal (invoice.payment_succeeded)', function () {
this.timeout(30000)
beforeEach(function (done) {
createSubscription(server, {planId}, function (err, resp) {
if (err) return done(err)
const {user, jwt, subscription} = resp
console.log(user, jwt)
testUser = user
testToken = jwt
testSubscription = subscription
done()
})
})
beforeEach(function (done) {
verifySignatureStub = sinon.stub(signatureMiddleware, 'verifySignature')
rawStub = sinon.stub(bp, 'raw')
rawStub.callsFake(function (req, res, next) {
console.log('bp raw')
return next()
})
verifySignatureStub.callsFake(function (req, res, next) {
const {customerId} = testUser.stripe
const subscriptionId = testSubscription.id
console.log('fake verify')
req.event = generateEventFromMock('invoice.payment_failed', {subscriptionId, customerId, planId})
return next()
})
done()
})
beforeEach(function (done) {
decache('../../../index')
server = require('../../../index')
const {customerId} = testUser.stripe
const {id: subscriptionId} = testSubscription
console.log(`emitting payment succeeded with ${customerId}, ${subscriptionId} ${planId}`)
emitPaymentSucceeded(server, testToken, function (err, response) {
if (err) return done(err)
done()
})
})
afterEach(function (done) {
verifySignatureStub.restore()
done()
})
it('Date subscription will renew gets set to a valid number roughly one month', function () {
// Not even getting here becasue calling the original function contains verifyMiddleware which should be replaced
})
it('Current period end is modified')
it('An invoice for the new starting period is generated')
it('Subscription status keeps active')
})
背景(请填写以下信息):
所有运行在Node 8上,我正在使用摩卡进行测试,并使用脏柴进行了设置。
这些是我的开发依赖项:
"devDependency":{"base 64url":"^2.0.0","cross-env":"^5.0.5","decache":"^4.4.0","kuly-chai":"^2.0.1","faker":"^4.1.0","google-auth-Library":"^0.12.0","googleapis":"^23.0.0","极简主义者":"^1.2.0","mocha":"^5.2.0","no恶魔":"^1.12.0","nyc":"^11.2.1","sinon":"^6.1.5","标准":"^10.0.3","stripe-local":"^0.1.1"}
https://github.com/sinonjs/sinon/issues/1889
根据经验,应在每次测试中设置存根,即在每个测试之前的或它之前的
中,而不是在
之前的
中。在这里,它们似乎不包含每个测试逻辑,但它们可以,在这种情况下,它们不会像预期的那样使用之前的
后恢复存根,这是自动完成的。<最好使用代码>mocha sinon
将mocha与sinon sandbox集成,因此无需在每次
由于验证签名(verifySignature)是导出属性,而不是导出本身,因此签名中间件(signatureMiddleware)模块可以保持原样,但使用它的模块应该在预期使用验证签名(verifySignature)的测试中反缓存并重新导入。如果整个测试套件的行为应该相同,那么也应该在每个测试套件之前执行。E、 g.如果这些中间件直接用于应用程序模块,则它是:
const decache = require('decache');
...
describe(() => {
let app;
beforeEach(() => {
verifySignatureStub = sinon.stub(signatureMiddleware, 'verifySignature');
...
});
beforeEach(() => {
decache('./app');
app = require('./app');
});
我正在尝试使用typescript、mocha、sinon和chai http为我的express路由器编写一个集成测试。这个路由器使用我编写的自定义中间件,它检查头中的JWT。 理想情况下,我想存根我的authMiddleware,这样我就可以控制它的行为,而不必为每个测试用例提供有效/无效的JWT。 当我尝试在测试中存根时,我意识到Express使用的实际实现,而不是模拟的实现。 在模拟aut
我有一个Redux操作,它本身分派了另外两个操作。每个操作都是从导入的函数中检索的。一个来自本地模块,另一个来自外部库。 在我的测试中,我使用一个 沙箱来存根函数,但只有两个测试通过。我期待所有3个都通过。 最后一个预期失败,错误为: TypeError:[Function:functionB]不是间谍或对间谍的调用! 当我将功能输出到控制台时,我得到了这个,这似乎与Babel导入导出导出的方式有
我有一个带有路由器的express应用程序,我想与Sinon一起测试。我无法成功模拟传递到请求处理程序的参数,希望能得到一些帮助。 这是我当前使用Mocha、Sinon、Chai的测试设置
问题内容: 我希望对我当前正在测试的文件中使用的函数存根。像这样的解构需要此函数: 测试时,永远不会调用存根,而是继续调用实函数。但是当我“正常”地要求它时(即:不破坏结构) 然后正确使用存根,一切正常 我感觉这是因为解构的工作原理以及直接对对象属性而不是对函数进行存根的事实。无论如何,如果您能为我提供一些见解,我将不胜感激! 问题答案: 使用从从属模块进行解构时,对模块的方法进行插桩的原因很简单
所以我在用摩卡酵素Sinon测试React app。我试图用if语句测试一个函数,其中有一个对另一个函数的调用。我的目标是输入if语句,但是存根第二个函数调用。代码如下: 所以我想输入if语句,但不调用getUsers()函数。我该怎么做?我正在监视SearchChange(),如下所示: 期待听到,谢谢!
如果我已经通过< code > var a = sinon . createstuinstance(my contractor)创建了一个实例。 如何替换其中一个存根函数,如 。 我这样做的主要原因是想实现这个提到的多个回调解决方法