测试函数时替换变量的值
Replace the value of a variable when testing a function
我有一个记录用户数据并给出唯一标识符的路由。
router.post('/', (req, res) => {
const user_email = req.body.user_email;
const username = req.body.username;
const user_phone = req.body.user_phone;
const service_sid = serviceSidVerification(user_phone);
const entry_id = uuidv4();
const user = new User(
username,
user_email,
user_password,
user_phone,
entry_id,
service_sid,
);
await db.query(user.setUser());
return res.status(200).json({
entry_id: entry_id,
});
});
我想为这条路线写一个测试。我需要用我自己的函数替换 serviceSidVerification()
函数中的 return 值。例如,12345
。如何通过测试来完成?
这就是我的测试结果。
describe(`Users Test`, () => {
describe('POST /api/users/', () => {
it(`Should create user`, (done) => {
let stub = sinon.createStubInstance(server);
stub.getEvents.returns('12345')
chai
.request(server)
.post('/api/users/')
.send({ 'username': faker.internet.userName() })
.send({ 'user_email': faker.internet.email() })
.send({ 'user_password': faker.internet.password() })
.send({ 'user_phone': faker.phone.phoneNumber() })
.end((err, res) => {
expect(res).have.status(200);
expect(res.body).have.property('entry_id');
expect(res.body.entry_id).to.be.an('string');
done();
});
});
});
});
});
...
function ServiceSidVerification(phone) {
const client = new twilio(config.smsVerify.accountSid, config.smsVerify.authToken);
const service_sid = (await client.verify.services
.create({
friendlyName: config.smsVerify.title,
codeLength: config.smsVerify.codeLength,
})
).sid;
const sid = (await client.verify.services(service_sid).verifications
.create({
to: phone,
channel: 'sms'
})
).sid;
return { service_sid, sid };
}
如何测试我的路线?
要更改 serviceSidVerification()
的行为,您可以使用 sinon。如果您决定采用这种方式,则必须在测试文件 + 模块中要求 sinon
,其中 serviceSidVerification
所在。然后,在执行测试之前,您必须执行以下操作:
const stubServiceSidVerification = sinon.stub(yourModule, 'serviceSidVerification')
.returns('yourValue');
请注意,如果您使用 sinon
,serviceSidVerification
应作为对象的 属性 导出,而不是作为独立函数导出,因为此 issue.
另一种选择是使用 proxyquire。
我有一个记录用户数据并给出唯一标识符的路由。
router.post('/', (req, res) => {
const user_email = req.body.user_email;
const username = req.body.username;
const user_phone = req.body.user_phone;
const service_sid = serviceSidVerification(user_phone);
const entry_id = uuidv4();
const user = new User(
username,
user_email,
user_password,
user_phone,
entry_id,
service_sid,
);
await db.query(user.setUser());
return res.status(200).json({
entry_id: entry_id,
});
});
我想为这条路线写一个测试。我需要用我自己的函数替换 serviceSidVerification()
函数中的 return 值。例如,12345
。如何通过测试来完成?
这就是我的测试结果。
describe(`Users Test`, () => {
describe('POST /api/users/', () => {
it(`Should create user`, (done) => {
let stub = sinon.createStubInstance(server);
stub.getEvents.returns('12345')
chai
.request(server)
.post('/api/users/')
.send({ 'username': faker.internet.userName() })
.send({ 'user_email': faker.internet.email() })
.send({ 'user_password': faker.internet.password() })
.send({ 'user_phone': faker.phone.phoneNumber() })
.end((err, res) => {
expect(res).have.status(200);
expect(res.body).have.property('entry_id');
expect(res.body.entry_id).to.be.an('string');
done();
});
});
});
});
});
...
function ServiceSidVerification(phone) {
const client = new twilio(config.smsVerify.accountSid, config.smsVerify.authToken);
const service_sid = (await client.verify.services
.create({
friendlyName: config.smsVerify.title,
codeLength: config.smsVerify.codeLength,
})
).sid;
const sid = (await client.verify.services(service_sid).verifications
.create({
to: phone,
channel: 'sms'
})
).sid;
return { service_sid, sid };
}
如何测试我的路线?
要更改 serviceSidVerification()
的行为,您可以使用 sinon。如果您决定采用这种方式,则必须在测试文件 + 模块中要求 sinon
,其中 serviceSidVerification
所在。然后,在执行测试之前,您必须执行以下操作:
const stubServiceSidVerification = sinon.stub(yourModule, 'serviceSidVerification')
.returns('yourValue');
请注意,如果您使用 sinon
,serviceSidVerification
应作为对象的 属性 导出,而不是作为独立函数导出,因为此 issue.
另一种选择是使用 proxyquire。