构造函数的存根方法 属性
Stub method of a constructor property
我正在尝试对使用 twilio-node 包发送 SMS 消息的功能进行单元测试。我要测试的函数,无论是传入的参数还是调用的时间,都是 Twilio.prototype.messages.create
.
sendText.ts
const twilio = new Twilio('ACfakeName', 'SomeAuthToken');
// Need to stub this guy
try {
await twilio.messages.create({body: 'something', to: `1234567890`, from: '1234567890' });
}
catch (e) {
console.log('An error while sending text', e);
}
sendText.spec.ts
twilioCreateStub = sinon.stub(Twilio.prototype.messages, 'create');
it('should call twilio.messages.create() once', async () => {
try {
await sendText();
}
catch (e) {
fail('This should not fail.')
}
expect(twilioCreateStub.callCount).to.equal(1);
});
运行 这样就没有通过 callCount
为 0 的测试。我不确定 mocha 是如何运行这些的,但似乎如果有测试失败,它不会显示任何内容日志。如果我删除 expect
部分,似乎真正的 twilio.messages.create
被调用,因为我得到以下日志:
An error while sending text { [Error: The requested resource /2010-04-01/Accounts/ACfakeName/Messages.json was not found]
status: 404,
message:
'The requested resource /2010-04-01/Accounts/ACfakeName/Messages.json was not found',
code: 20404,
moreInfo: 'https://www.twilio.com/docs/errors/20404',
detail: undefined }
我也试过 sinon.createStubInstance
也有类似的结果。我看不到任何迹象表明我正在对深层嵌套方法进行存根。
我会将 Twillio 实例注入您的 class。然后在测试时你可以制作 class:
的存根
class myClass{
constructor(twillio){
this.twilio = twilio;
}
//functions using twillio here
}
然后你可以做一个存根:
const twilioStub = {messages: {create: sinon.stub()}}; //You might want to give this more functions and put it in a seperate file
myClass = new MyClass(twiliostub);
//call function on myClass using twilio
expect(twilioStub.messages.create.callCount).to.equal(1);
我正在尝试对使用 twilio-node 包发送 SMS 消息的功能进行单元测试。我要测试的函数,无论是传入的参数还是调用的时间,都是 Twilio.prototype.messages.create
.
sendText.ts
const twilio = new Twilio('ACfakeName', 'SomeAuthToken');
// Need to stub this guy
try {
await twilio.messages.create({body: 'something', to: `1234567890`, from: '1234567890' });
}
catch (e) {
console.log('An error while sending text', e);
}
sendText.spec.ts
twilioCreateStub = sinon.stub(Twilio.prototype.messages, 'create');
it('should call twilio.messages.create() once', async () => {
try {
await sendText();
}
catch (e) {
fail('This should not fail.')
}
expect(twilioCreateStub.callCount).to.equal(1);
});
运行 这样就没有通过 callCount
为 0 的测试。我不确定 mocha 是如何运行这些的,但似乎如果有测试失败,它不会显示任何内容日志。如果我删除 expect
部分,似乎真正的 twilio.messages.create
被调用,因为我得到以下日志:
An error while sending text { [Error: The requested resource /2010-04-01/Accounts/ACfakeName/Messages.json was not found]
status: 404,
message:
'The requested resource /2010-04-01/Accounts/ACfakeName/Messages.json was not found',
code: 20404,
moreInfo: 'https://www.twilio.com/docs/errors/20404',
detail: undefined }
我也试过 sinon.createStubInstance
也有类似的结果。我看不到任何迹象表明我正在对深层嵌套方法进行存根。
我会将 Twillio 实例注入您的 class。然后在测试时你可以制作 class:
的存根class myClass{
constructor(twillio){
this.twilio = twilio;
}
//functions using twillio here
}
然后你可以做一个存根:
const twilioStub = {messages: {create: sinon.stub()}}; //You might want to give this more functions and put it in a seperate file
myClass = new MyClass(twiliostub);
//call function on myClass using twilio
expect(twilioStub.messages.create.callCount).to.equal(1);