使用 ava 和 proxyquire 将模块加载到 sinon 后无法存根方法
Unable to stub method after module is loaded with sinon using ava and proxyquire
我正在尝试对 OtpService
中定义的方法 doSendOtp
进行存根,但存根不起作用。
它适用于 mocha,但我不知道为什么它不适用于 ava。
我正在使用 .serial
进行此操作,但它也无济于事。
/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const app = require('../src/app');
const OtpService = require('../src/components/auth/otp.service');
test.before(async (t) => {
t.context.server = request(app);
t.context.baseUrl = '/api/v1/auth';
});
test.after.always((t) => {
delete require.cache[require.resolve('../src/app')]; // kills server
});
test.serial('Send OTP', async (t) => {
const { server, baseUrl } = t.context;
const sendOtpStub = sinon.stub(OtpService, 'doSendOtp');
const response = await server.get(`${baseUrl}/send-otp?phone=7845489898`)
.expect(200);
expect(sendOtpStub.calledOnce).to.be.true;
expect(response).to.be.a('Object');
});
测试失败错误。
Send OTP
tests\auth.test.js:25
24: .expect(200);
25: expect(sendOtpStub.calledOnce).to.be.true;
26: expect(response).to.be.a('Object');
Rejected promise returned by test. Reason:
AssertionError {
actual: false,
expected: true,
message: 'expected false to be true',
showDiff: true,
}
» tests/auth.test.js:25:39
─
1 test failed
npm ERR! Test failed. See above for more details.
更新:
我也尝试使用 proxyquire
但仍然无法存根 OtpService
的 doSendOtp
。
/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const proxyquire = require('proxyquire');
const app = require('../src/app');
test.before(async (t) => {
const sendOtpStub = sinon.stub().resolves({});
proxyquire('../src/components/auth/auth.module', {
'./otp.service': {
doSendOtp: sendOtpStub,
},
});
t.context.stubs = {
sendOtpStub,
};
t.context.baseUrl = '/api/v1/auth';
t.context.server = request(app);
});
test.after.always((t) => {
delete require.cache[require.resolve('../src/app')]; // kills
});
test.serial('Send OTP', async (t) => {
const { server, baseUrl, stubs } = t.context;
const response = await server.get(`${baseUrl}/send-otp?phone=8576863491`)
.expect(200);
expect(stubs.sendOtpStub.calledOnce).to.be.true;
expect(response).to.be.a('Object');
});
// test.serial('Login', async (t) => {
// })
我修好了,我在加载模块后存根。我需要在加载模块之前存根。
/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
require('dotenv').config();
// dependency to be stubbed
const OtpService = require('../src/components/auth/otp.service');
// stubs
const sendOtpStub = sinon.stub(OtpService, 'doSendOtp').resolves({});
const app = require('../src/app');
test.before(async (t) => {
t.context.stubs = {
sendOtpStub,
};
t.context.baseUrl = '/api/v1/auth';
t.context.server = request(app);
});
test.after.always((t) => {
delete require.cache[require.resolve('../src/app')]; // kills server
});
test.serial('Send OTP', async (t) => {
const { server, baseUrl, stubs } = t.context;
const res = await server.get(`${baseUrl}/send-otp?phone=77979879878`);
t.is(res.status, 200);
t.true(stubs.sendOtpStub.calledOnce);
t.true(typeof res === 'object');
});
我正在尝试对 OtpService
中定义的方法 doSendOtp
进行存根,但存根不起作用。
它适用于 mocha,但我不知道为什么它不适用于 ava。
我正在使用 .serial
进行此操作,但它也无济于事。
/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const app = require('../src/app');
const OtpService = require('../src/components/auth/otp.service');
test.before(async (t) => {
t.context.server = request(app);
t.context.baseUrl = '/api/v1/auth';
});
test.after.always((t) => {
delete require.cache[require.resolve('../src/app')]; // kills server
});
test.serial('Send OTP', async (t) => {
const { server, baseUrl } = t.context;
const sendOtpStub = sinon.stub(OtpService, 'doSendOtp');
const response = await server.get(`${baseUrl}/send-otp?phone=7845489898`)
.expect(200);
expect(sendOtpStub.calledOnce).to.be.true;
expect(response).to.be.a('Object');
});
测试失败错误。
Send OTP
tests\auth.test.js:25
24: .expect(200);
25: expect(sendOtpStub.calledOnce).to.be.true;
26: expect(response).to.be.a('Object');
Rejected promise returned by test. Reason:
AssertionError {
actual: false,
expected: true,
message: 'expected false to be true',
showDiff: true,
}
» tests/auth.test.js:25:39
─
1 test failed
npm ERR! Test failed. See above for more details.
更新:
我也尝试使用 proxyquire
但仍然无法存根 OtpService
的 doSendOtp
。
/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const proxyquire = require('proxyquire');
const app = require('../src/app');
test.before(async (t) => {
const sendOtpStub = sinon.stub().resolves({});
proxyquire('../src/components/auth/auth.module', {
'./otp.service': {
doSendOtp: sendOtpStub,
},
});
t.context.stubs = {
sendOtpStub,
};
t.context.baseUrl = '/api/v1/auth';
t.context.server = request(app);
});
test.after.always((t) => {
delete require.cache[require.resolve('../src/app')]; // kills
});
test.serial('Send OTP', async (t) => {
const { server, baseUrl, stubs } = t.context;
const response = await server.get(`${baseUrl}/send-otp?phone=8576863491`)
.expect(200);
expect(stubs.sendOtpStub.calledOnce).to.be.true;
expect(response).to.be.a('Object');
});
// test.serial('Login', async (t) => {
// })
我修好了,我在加载模块后存根。我需要在加载模块之前存根。
/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
require('dotenv').config();
// dependency to be stubbed
const OtpService = require('../src/components/auth/otp.service');
// stubs
const sendOtpStub = sinon.stub(OtpService, 'doSendOtp').resolves({});
const app = require('../src/app');
test.before(async (t) => {
t.context.stubs = {
sendOtpStub,
};
t.context.baseUrl = '/api/v1/auth';
t.context.server = request(app);
});
test.after.always((t) => {
delete require.cache[require.resolve('../src/app')]; // kills server
});
test.serial('Send OTP', async (t) => {
const { server, baseUrl, stubs } = t.context;
const res = await server.get(`${baseUrl}/send-otp?phone=77979879878`);
t.is(res.status, 200);
t.true(stubs.sendOtpStub.calledOnce);
t.true(typeof res === 'object');
});