0

I can't find a user through ID in the end method. However I can find a user outside the it method. But then I can't use the res.body._id variable to give it as a argument to the find method.

Below you can see my code:

describe("A user registers", function () {
    var id = "asd";
    it('should create a SINGLE user on /api/register POST', function (done) {
        //calling REGISTER api
        server
                .post('/api/register')
                .send({
                    name: "John Doe",
                    username: "john",
                    password: "open",
                    confirm_password: "open"
                })
                .expect("Content-type", /json/)
                .expect(200)
                .end(function (err, res) {
                    //TO DO: compare created_at and updated_at
                    var data = {
                        _id: res.body._id, //the _id is dynamic because it's just created
                        name: "John Doe",
                        username: "john",
                        admin: false
                    };
                    res.status.should.equal(200);
                    assert.deepEqual(res.body, data);
                    //TO DO: check if user is inserted in the database through using the ID to find the user
                    id = res.body._id;
                    done();
                });
    });
    User.find({username: "john"}, function (err, users) {
        if (err)
            return console.error(err);
        console.log(users);
        assert.equal(users.length, 1);
    });
});

Also moving the find method inside the end method won't work. Because I don't see anything when I use: console.log(users);

Below you can find the full source code of the file in case are you interested:

link: http://pastebin.com/HnhxJWpW

Can someone maybe help me, please?

superkytoz
  • 1,267
  • 4
  • 23
  • 43

0 Answers0