Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mocha测试post函数_Javascript_Node.js_Testing_Mocha.js - Fatal编程技术网

Javascript Mocha测试post函数

Javascript Mocha测试post函数,javascript,node.js,testing,mocha.js,Javascript,Node.js,Testing,Mocha.js,嗯,如果我犯了一些愚蠢的错误,那就再检查一遍,不过看起来不像。我只是想通过这个测试,但它一直给我一个超时错误。这个模块应该可以正常工作,它可以正确地发送邮件,但是摩卡会一直超时 // describe('POST /api/mail', function() { // it('should successfully send mail', function(done) { // request(app) // .post('/api/mail') //

嗯,如果我犯了一些愚蠢的错误,那就再检查一遍,不过看起来不像。我只是想通过这个测试,但它一直给我一个超时错误。这个模块应该可以正常工作,它可以正确地发送邮件,但是摩卡会一直超时

// describe('POST /api/mail', function() {
//  it('should successfully send mail', function(done) {
//      request(app)
//          .post('/api/mail')
//          .send(form)
//          .expect(200)
//          .end(function(err, res) {
//              if (err) return done(err);
//              done();
//          });
//  });
// });
这是正在测试的实际功能

'use strict';
var transporter = require('./transporter.js').transporter;

exports.sendMail = function(req, res){
    // setup e-mail data with unicode symbols
    var mailOptions = {
        from: req.body.name + ' <'+req.body.email+'>',
        to: 'test@gmail.com',
        subject: req.body.subject,
        text: req.body.message
    };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function(err, info){
        if(err){
            res.status(400); //error
        }else{
            res.status(200); //success
        }
    });
};
“严格使用”;
var transporter=require('./transporter.js').transporter;
exports.sendMail=函数(请求、回复){
//使用unicode符号设置电子邮件数据
var mailpoptions={
from:req.body.name+'',
致:'test@gmail.com',
主题:req.body.subject,
文本:req.body.message
};
//使用定义的传输对象发送邮件
transporter.sendMail(邮件选项,函数(错误,信息){
如果(错误){
res.status(400);//错误
}否则{
res.status(200);//成功
}
});
};

我想摩卡正在等待通过回调发送邮件的结果。我有一个类似的sendMail,在应用程序中使用nodeEmailer.js:

    function send(fr, to, sj, msg, callback){

    //...

    var transport = nodemailer.createTransport();

    console.log("Message content: "+msg);

    transport.sendMail({from:fr, to:to, subject: sj, text: "\r\n\r\n" + msg},
        function(err,response){
            if(err){
                callback(err);          
            }else{          
                callback(response);
            }
        });
};
在我的测试中:

describe('when this example is tested',function(done){
it('should be sending an email', function(done){            
    mailSender.sendMail('test@test.es', 'Test', 'Test text', function(sent){            
        sent.should.be.ok;  
        done();
    });     
});
您在回调中收到发送的消息,然后Mocha可以访问done()方法来指示测试已经完成

此外,还可以使用来测试端点。应该是这样的:

it('should return 200 on /api/mail', function(done) {

    supertest('http://localhost:3000').post('/api/mail').expect(200)
    .end(
        function(err, res) {
            if (err) {
                return done(err);
            }
            done();
        });
});