【问题标题】:how do i use should with mocha and mongoose?我应该如何与摩卡和猫鼬一起使用?
【发布时间】:2012-10-30 12:14:44
【问题描述】:

我在运行测试时在 save() 方法中不断收到错误。

var User = require('../../models/user')
, should = require('should');

describe('User', function(){
  describe('#save()', function(){
    it('should save without error', function(done){
      var user = new User({
        username    : 'User1'
        , email     : 'user1@example.com'
        , password  : 'foo'
      });
      user.save(function(err, user){
        if (err) throw err;

        it('should have a username', function(done){
          user.should.have.property('username', 'User1');
          done();
        });
      });
    })
  })

})

这是错误:

$ mocha test/unit/user.js

  ․

  ✖ 1 of 1 test failed:

  1) User #save() should save without error:
     Error: timeout of 2000ms exceeded
      at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:1
61:14)
      at Timer.list.ontimeout (timers.js:101:19)

【问题讨论】:

  • 我们如何从实际函数中设置username 属性,以便我们在user.should.have.property('username', 'User1'); 中获取用户名属性?我们可以在实际功能中以res.send({username: 'User1'})res.render('home', {username: 'User1'}) 发送吗?

标签: unit-testing node.js mongoose mocha.js


【解决方案1】:

您可以嵌套描述,但不能嵌套测试。每个测试都是独立的,因此当您查看结果时,您可以看到失败的地方 - 保存或没有用户名属性。例如,在您上面的代码中,由于没有 done(),因此无法通过“应该保存且没有错误”测试失败。这也是上面的代码超时的原因:mocha 找不到“应该保存而没有错误”测试的 done()。

虽然能够嵌套描述是非常强大的。在每个描述中,您可以有一个 before、beforeEach、after 和 afterEach 语句。有了这些,您可以实现上面尝试的嵌套。如果您想阅读这些声明,请参阅 mocha 文档以获取更多信息。

我会写你想要实现的方式如下:

var User = require('../../models/user')
    , should = require('should')
    // this allows you to access fakeUser from each test
    , fakeUser;

describe('User', function(){
  beforeEach(function(done){
    fakeUser = {
      username    : 'User1'
      , email     : 'user1@example.com'
      , password  : 'foo'
    }
    // this cleans out your database before each test
    User.remove(done);
  });

  describe('#save()', function(){
    var user;
    // you can use beforeEach in each nested describe
    beforeEach(function(done){
      user = new User(fakeUser);
      done();
    }

    // you are testing for errors when checking for properties
    // no need for explicit save test
    it('should have username property', function(done){
      user.save(function(err, user){
        // dont do this: if (err) throw err; - use a test
        should.not.exist(err);
        user.should.have.property('username', 'User1');
        done();
      });
    });

    // now try a negative test
    it('should not save if username is not present', function(done){
      user.username = '';
      user.save(function(err, user){
        should.exist(err);
        should.not.exist(user.username);
        done();
      });
    });
  });
});

【讨论】:

  • 当我运行这个测试时,我得到:` it('should have username property', function(done){ ^^ SyntaxError: Unexpected identifier` it doesn't like "it(" `跨度>
  • 如果我添加另一个描述作为第一个描述的第二个孩子,那么 User.rmeove() 没有任何效果。
  • 我们如何从实际函数中设置username 属性,以便我们在user.should.have.property('username', 'User1'); 中获取用户名属性?我们可以在实际功能中以res.send({username: 'User1'})res.render('home', {username: 'User1'}) 发送吗?
  • @chovy 你介意分享一下你是如何解决ìt 错误的吗?
  • @edelans 我不记得了,我想可能是剪切粘贴错误,但这是我现在使用的:gist.github.com/chovy/e637f4f84436cfafa0ce
猜你喜欢
  • 2014-11-22
  • 2012-12-03
  • 2016-12-26
  • 1970-01-01
  • 2013-05-24
  • 2016-02-05
  • 2013-07-10
  • 1970-01-01
  • 2017-06-23
相关资源
最近更新 更多