摩卡测试与Mongoose挂起

时间:2015-06-22 21:52:34

标签: javascript node.js mongoose mocha

我正在使用mochachai来测试getUserProfile

describe("Testing the getUserInfo methods for the model", function() {

    describe("testing getProfileFromModel function", function(done) {
        var user;
        var getProfileFromModel;
        var savePromise;

        beforeEach(function(done) {
            mongoose.connect("mongodb://localhost/login_test");
            mongoose.connection.on("connected", function() {
                console.log("connected to mongoose");
            });
            mongoose.connection.on("disconnected", function(){
                console.log("disconnected from mongoose");
            });

            mongoose.connection.on("error", function(){
                console.log("error connecting to mongoose");
            });

            mongoose.connection.once("open",function() {
            user = new User({
                local:{
                    email:"test@test.com",
                    password:"keyboardcat"
                }
            });
            mongoose.connection.once("open",function() {
                user = new User({
                   local:{
                        email:"test@test.com",
                        password:"keyboardcat"
                   }
                });
                savePromise = user.saveAsync()
                    .then(function(data){
                       console.log("saved",data);
                    })
                    .catch(function(err){
                        throw err;
                    })
                    .finally(function(){
                        done();
                    });
            });
        });
            getProfileFromModel = getUserProfile.getProfileFromModel;   
        });

        afterEach(function(done){
            user.removeAsync({_id:user._id})
                .then(function(data){
                    console.log("model has been deleted",data);     
                })
                .catch(function(err){
                    throw err;
                })
                .finally(function(){
                    mongoose.connection.close();
                    done(); 
                })
        });

        it("is expected to return a user with a string parameters", function(done) {
            var id = user._id;
            var projection = "local.email";
            savePromise.then(function(data){
                var getProfile = getProfileFromModel(User);
                getProfile(id,projection)
                    .then(function(data) {
                        expect(data).to.equal({
                            email:"test@test.com"
                        });
                    })
                    .catch(function(err) {
                        throw err;
                    })
                    .finally(function(){
                        done();
                    })
            });
        });
    });

});
});

使用node-inspectort进行调试时,我发现beforeEach被执行,之后它与mongoose断开连接。

0 个答案:

没有答案