尝试使用sinon监视函数但出现错误

时间:2018-12-14 12:23:28

标签: node.js sinon

我有一个名为email的文件,它有一个名为sendemail的方法,该方法调用创建消息

<p>

测试文件如下所示

<input *ngIf="isEditable" type="text" ... [(ngModel)]="Name">
<span *ngIf="!isEditable"> {{Name}} </span>

我收到此错误

//email.js
const createEmail = function (to, from, subject, bodyText) {
   const emailLines = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
     "MIME-Version: 1.0\n",
     "Content-Transfer-Encoding: 7bit\n",
     "to: ", to, "\n",
     "from: ", from, "\n",
     "subject: ", subject, "\n\n",
     bodyText
   ].join('');
   return emailLines
 }


   const sendemail = function(to, from, subject, bodyText){
     const message = createemail(to, from, message);
     auth = getauth();
     sendmessagetogoogleapi(auth, message)
}
 module.exports.sendEmail = sendEmail;
 module.exports.createEmail = createEmail;

先谢谢大家。

1 个答案:

答案 0 :(得分:0)

在测试/规格文件中,您需要导入email.js。

let email = require('email.js')
let createEmailStub = sinon.stub(email, "createEmail")
email.sendEmail('otest@domain.de',test2@domain','test','this is test',['https://www.googleapis.com/auth/gmail.send'])
expect(createEmailStub.called).to.be.true;
sinon.assert.calledOnce(creatEmailStub);
createEmailStub.restore(); // you need to restore stub after use. Or you can put it in afterEach function.
done();
相关问题