gpt4 book ai didi

node.js - 使用 ava 和 proxyquire 用 sinon 加载模块后无法 stub 方法

转载 作者:行者123 更新时间:2023-12-03 12:18:27 24 4
gpt4 key购买 nike

我正在尝试 stub 方法doSendOtp定义于 OtpService但 stub 不起作用。
它适用于 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但仍然无法 stub doSendOtpOtpService .
/* 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) => {

// })

最佳答案

我修复了它,加载模块后我正在 stub 。我需要在加载模块之前 stub 。

/* 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');
});

关于node.js - 使用 ava 和 proxyquire 用 sinon 加载模块后无法 stub 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64221850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com