无法通过ID找到用户

时间:2016-07-09 22:34:45

标签: javascript unit-testing mongoose mocha supertest

我无法通过end方法通过ID找到用户。但是,我可以找到it方法之外的用户。但是,我无法使用res.body._id变量将其作为find方法的参数。

下面你可以看到我的代码:

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

同样在find方法中移动end方法也无法正常工作。因为我在使用时看不到任何内容:console.log(users);

以下是您感兴趣的文件的完整源代码:

link:http://pastebin.com/HnhxJWpW

有人可以帮助我吗?

0 个答案:

没有答案
相关问题