mocha:使用电子邮件验证框架

时间:2017-04-02 10:27:33

标签: express mocha email-verification

我尝试测试使用电子邮件验证框架的注册路由器post功能,但是mocha向我显示以下错误消息: TypeError:无法读取null

的属性'findOne'

这是我的代码:

调用错误的函数:

nev.resendVerificationEmail(email,(err,userExist) =>{
if(err){
      return res.status(404).send('Error : resending verification email failed');}
if(userExist){
      res.json({message : 'An email has been sent to you , again. Please check it to verify your account'});
}else{
      res.json({message : 'Your verification code has expired . Please sign up again'});

,这是resendVerificationEmail函数的实现

var resendVerificationEmail = function(email, callback) {
var query = {};
query[options.emailFieldName] = email;

options.tempUserModel.findOne(query, function(err, tempUser) { //this the error handler I guess
  if (err) {
    return callback(err, null);
  }

  // user found (i.e. user re-requested verification email before expiration)
  if (tempUser) {
    // generate new user token
    tempUser[options.URLFieldName] = randtoken.generate(options.URLLength);
    tempUser.save(function(err) {
      if (err) {
        return callback(err, null);
      }

      sendVerificationEmail(getNestedValue(tempUser, options.emailFieldName), tempUser[options.URLFieldName], function(err) {
        if (err) {
          return callback(err, null);
        }
        return callback(null, true);
      });
    });

  } else {
    return callback(null, false);
  }
});

};

这是我的规范

describe(' SignUp : /POST Test with fake client request : ', () => {
    let req, res, statusCode, sendData,user;
    beforeEach((done) => {
        SignupModal.remove({}, (err) => {
            done();
        });

        user = {
            firstName : 'ben',
            secondName : 'wissem',
            username : 'wiss',
            email : 'xy@zt.sq',
            password : 'wissem'
        };
        res = {
            json: function (code, data) {
                statusCode = code;
                sendData = data;
            }
        };
    });

    it('should send 200 code', () => {
        chai.request(server)
            .post('/user/signup')
            .send(user)
            .end((err, res) => {
                res.statusCode.should.equal(200);
            });
    });

任何人都可以帮助我吗?!

1 个答案:

答案 0 :(得分:0)

如果您希望端到端测试电子邮件验证尝试EmailE2E.com,它是一个免费的API,可让您发送和接收来自随机生成的收件箱的电子邮件。它非常适合Firebase,Amazon Cognito或其他在注册时使用电子邮件验证码的OAuth提供商。它还有一个可以与Mocha一起使用的Javascript客户端。