Javascript Sinon存根不使用module.exports={f1,f2}
我有这个文件,发送otp如下 OtpService.js 下面是使用这些方法的控制器otp.jsJavascript Sinon存根不使用module.exports={f1,f2},javascript,node.js,integration-testing,sinon,supertest,Javascript,Node.js,Integration Testing,Sinon,Supertest,我有这个文件,发送otp如下 OtpService.js 下面是使用这些方法的控制器otp.js const { verifyOTP, generateOTP } = require('../../services/OtpService') const verify = async function(req, res) { const {error, data} = await generateOTP(req.query.phone) } const send = async functi
const { verifyOTP, generateOTP } = require('../../services/OtpService')
const verify = async function(req, res) {
const {error, data} = await generateOTP(req.query.phone)
}
const send = async function(req, res) {
const {error, data} = await verifyOTP(req.query.phone, req.query.otp)
}
module.exports = {
send,
verify
}
下面是测试文件otp.test.js
const sinon = require('sinon');
const expect = require('chai').expect
const request = require('supertest')
const OtpService = require('../../src/services/OtpService')
console.log(OtpService)
describe('GET /api/v1/auth/otp', function() {
let server
let stub
const app = require('../../src/app')
stub = sinon.stub(OtpService, 'generateOTP').resolves({
error: null,
data: "OTP Sent"
})
server = request(app)
it('should generate OTP', async () => {
const result = await server
.get('/api/v1/auth/otp/send?phone=7845897889')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
console.log(result.body)
expect(stub.called).to.be.true
expect(result).to.be.a('Object')
});
});
上述方法不起作用,在控制器中调用时,它不会中断generateTP
和verifyOTP
方法
但是,如果我在otp.test.js
中调用OtpService.generateOTP()
,那么它在那里工作,但在控制器中不工作
希农是怎么在这里工作的
我在这里感到困惑。
需要应用程序然后存根是正确的还是先存根然后需要正确的
两种方法我都试过,但都不管用。
我还尝试使用了before()和beforeach()
下面是我的文件夹结构
otp.js
(控制器)在这里controller->AuthController->otp.js
otp.test.js
在这里test->auth->otp.test.js
OtpService.js
就在services
更新
我发现了问题所在。
如果我不在控制器中使用破坏功能,一切正常。因此,使用OtpService.generateOTP
是可行的
问题在于物体的破坏
const { verifyOTP, generateOTP } = require('../../services/OtpService')
上面是正在运行之前的存根。因此verifyOTP
和generateOTP
已经引用了unsubbed
方法
我需要一个变通办法。我想使用销毁功能。我使用包来存根OtpService
模块。下面的示例是单元测试,但是您可以使用这种方式进行集成测试
例如
otp.js
:
const{verifyOTP,generateOTP}=require('./OtpService');
常量验证=异步函数(req、res){
返回generateTP(请求查询电话);
};
const send=异步函数(req、res){
返回verifyOTP(req.query.phone,req.query.otp);
};
module.exports={
送,
验证
};
OtpService.js
:
const generateOTP=async function(){
//
};
const verifyOTP=异步函数(){
//
};
module.exports={
生成TP,
验证OTP,
};
otp.test.js
:
const proxyquire=require('proxyquire');
const sinon=要求(“sinon”);
描述('60704684',()=>{
它('should send',async()=>{
常数otpServiceStub={
verifyOTP:sinon.stub(),
generateTP:sinon.stub(),
};
const{send}=proxyquire('./otp'{
“./OtpService”:otpServiceStub,
});
const mReq={query:{phone:'123',otp:'otp'};
常数mRes={};
等待发送(mReq、mRes);
sinon.assert.calledWithjustice(otpServiceStub.verifyOTP,'123','otp');
});
它('should verfiy',async()=>{
常数otpServiceStub={
verifyOTP:sinon.stub(),
generateOTP:sinon.stub().resolves({error:null,data:'fake data'}),
};
const{verify}=proxyquire('./otp'{
“./OtpService”:otpServiceStub,
});
constmreq={query:{phone:'123'}};
常数mRes={};
等待验证(mReq、mRes);
sinon.assert.calledWithjustice(otpServiceStub.generateOTP,'123');
});
});
单元测试结果和覆盖率报告:
60704684
✓ 应发送(1744ms)
✓ 应该是
2次通过(2秒)
---------------|---------|----------|---------|---------|-------------------
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s
---------------|---------|----------|---------|---------|-------------------
所有文件| 100 | 100 | 50 | 100 |
OtpService.js | 100 | 100 | 0 | 100 |
otp.js | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------
源代码:
const { verifyOTP, generateOTP } = require('../../services/OtpService')