NodeJS/Passport - 使用 mocha 和 superagent 测试用户登录 [英] NodeJS/Passport - Testing user login with mocha and superagent

查看:72
本文介绍了NodeJS/Passport - 使用 mocha 和 superagent 测试用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用基本 MEAN 堆栈构建了一个登录过程,并使用通行证进行身份验证.

I have built a login process with a basic MEAN stack and using passport for the authentication process.

我正在尝试设置测试以确保登录过程正常工作.这里的登录部分是我使用的代码:

I am trying to set up a test to make sure the login process is working. To do the login part here is the code I used:

  it ('login', function(done) {
    agent.post(config.web.vhost + '/login')
      .send({ email: 'bruce@wayne.inc', password: 'batman' })
      .end(function(err, res) {
      if (err) console.log('error' + err.message);

        res.should.have.status(200);

        done();
      });
  });

我收到以下错误:

没有错误电子邮件:bruce@wayne.inc․没有错误错误套接字挂断․双回调!

no error email: bruce@wayne.inc ․no error errorsocket hang up ․double callback!

  1 passing (2s)
  1 failing

  1) User login:
     Uncaught TypeError: Cannot read property 'should' of undefined

我知道我的路线和凭据很好,但我不知道这里有什么不工作.这是我进行用户测试的第一步,所以我可能没有做对.

I know my routes and credentials are good but I cant figure out whats not working here. This is my first steps with user testing so I am probably not getting something right.

这是我的其余测试:

var should = require("should");
var mongoose = require('mongoose');
var request = require('superagent');
var agent = request.agent();

var config = require("../settings/conf");
var dbUrl = require("../config/database.js");
var User = require('../server/models/user');

var db;

describe('User', function() {

before(function(done) {
  db = mongoose.connect(dbUrl.url);
    done();
  });

  after(function(done) {
     mongoose.connection.close()
    done();
  });

  beforeEach(function(done) {
    var user = new User({
      email: 'bruce@wayne.inc',
      password: 'batman',
      firstName: 'Bruce',
      lastName: 'Wayne'
    });

    user.save(function(err, user) {
      if (err) console.log('error' + err.message);
      else console.log('no error');

      done();
    });
  });

  it('find a user by username', function(done) {
    User.findOne({ email: 'bruce@wayne.inc' }, function(err, user) {
      user.email.should.eql('bruce@wayne.inc');
      console.log("   email: ", user.email)
      done();
    });
  });

  it ('login', function(done) {
    agent.post(config.web.vhost + '/login')
      .send({ email: 'bruce@wayne.inc', password: 'batman' })
      .end(function(err, res) {
      if (err) console.log('error' + err.message);

        res.should.have.status(200);

        done();
      });
  });

  afterEach(function(done) {
    User.remove({ email: 'bruce@wayne.inc' }, function() {
      done();
    });
  });
});

推荐答案

我注意到你看到的失败是 connect ECONNREFUSED -- 所以这与护照无关,只是你'在您的 agent.post 中没有成功连接到您的服务器.

I notice that the failure you're seeing is connect ECONNREFUSED -- so it's not anything about passport, it's just that you're not connecting successfully to your server in your agent.post.

我猜要么你的 config.web.vhost 值设置不正确,要么不是 superagent 正在寻找的正确形式(例如,superagent 可能需要一个完整的绝对 url 和 'http://' 在前面,而您的 vhost 值可能只是主机名).

I would guess either your config.web.vhost value isn't set correctly or isn't in the right form for what superagent is looking for (for instance, superagent may require a full absolute url with the 'http://' on the front, and your vhost value may only be the hostname).

这篇关于NodeJS/Passport - 使用 mocha 和 superagent 测试用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆