【问题标题】:NodeJS testing Database with Mocha and should.jsNodeJS 使用 Mocha 和 should.js 测试数据库
【发布时间】:2013-12-10 05:54:08
【问题描述】:

我正在使用 mocha 测试我的 NodeJS 应用程序,并且应该。虽然第一个测试运行顺利,但第二个测试失败(错误等于 null)。在这两个测试中,如果在回调中获得了有效用户(两者在 mongoose 中具有相同的 id)。测试显然不会等待数据库操作发生。

describe("User", function(){
    before(function (done) {
        // clear database
        model.UserModel.collection.remove(done);
    })

    it("should save", function(done){
        user1.save(function(error, user){
            should.not.exist(error);
            user.should.have.property("first_name", "Rainer");
            done();
        })
    })

    it("should not save duplicate user", function(done){
        user1.save(function(error, user){
            should.exist(error);
            done();
        })
    })
})


当我将第二个测试放在第一个测试的回调中时,它也不起作用。 我想测试重复键错误,但在给定的情况下无法实现。

【问题讨论】:

    标签: javascript node.js mongoose mocha.js should.js


    【解决方案1】:

    您似乎在重复使用user1 文档。

    在 Mongoose 中,您可以再次保存同一个文档(例如,在对其进行更改之后);这并不意味着保存了新文档,只是更新了旧文档。

    如果您想正确测试,您应该在第二次测试中创建一个新的文档实例(与第一个具有相同的属性)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多