NodeJS/Passport - Testing user login with mocha and superagent

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 07:33:07

问题


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();
      });
  });

I get the following error:

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.

Here is the rest of my test:

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();
    });
  });
});

回答1:


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.

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).



来源:https://stackoverflow.com/questions/24325181/nodejs-passport-testing-user-login-with-mocha-and-superagent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!