我该如何搭配摩卡咖啡和猫鼬使用? [英] how do i use should with mocha and mongoose?

查看:76
本文介绍了我该如何搭配摩卡咖啡和猫鼬使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行测试时,我在save()方法中始终遇到错误.

I keep getting an error in the save() method when I run the test.

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属性.例如,在上面的代码中,由于没有done(),因此它无法通过应保存无错误"测试而失败.这也是上面的代码超时的原因:摩卡无法找到应无错误保存"测试的done().

You can nest describes but not it tests. Each test is meant to be stand alone so when you are looking through your results you can see where it is failing - on the save or not having the username property. For example in your code above there is no way for it to fail the 'should save without error' test as there is no done(). Which is also the reason the code above is timing out: mocha cannot find the done() for the 'should save without error' test.

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

Being able to nest describes is very powerful though. Within each describe you can have a before, beforeEach, after and afterEach statement. With these you can achieve the nesting that you are attempting above. See the mocha docs for more information if you want to read up on these statements.

我将写出您想要达到的目标的方法如下:

The way I would write what you are trying to achieve is as follows:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆