Mocha / Chai: Unable to do login testing

0

I am writing tests for a login service. The test must pass if the login is correct. Before each test my test database is cleaned. That's why I first created a user and then I try to login. The expected behavior is to receive status 200 when the login is successful, but instead I am receiving status 401 which indicates that the user is not creating correctly.

describe("Users", () => {
   beforeEach(done => {
       User.remove({}, () => {
        done();
      });
   });

describe("/POST login", () => {
    it("should return successful login", done => {
        const user = new User({
            username: "test",
            email: "[email protected]",
            password: "testpass"
        });
        const user2 = {
            user: "test",
            password: "testpass"
        };
        user.save(err => {
            if (err) {
                done();
            }
            chai.request(server)
                .post("/users/login")
                .send(user2)
                .end((err, res) => {
                    expect(res).to.has.status(200);
                    done();
                });
        });  

    });

Test result:

  

Uncaught AssertionError: expected {Object (_events, _eventsCount, ...)} to have status code 200 but got 401

Any idea what happens here?

    
asked by Cesar Jr Rodriguez 14.12.2018 в 12:33
source

0 answers