【问题标题】:Avoiding Mocha timeout on assertion error with SuperTest?使用 SuperTest 避免在断言错误时出现 Mocha 超时?
【发布时间】:2014-12-31 00:04:53
【问题描述】:

我有一些 Sails.js API 测试(使用 Mocha)利用 SuperTest 的 .end() 方法在响应上运行一些 Chai 断言。

我在断言之后调用测试的done() 回调,但如果抛出断言错误,测试就会超时。

我可以将断言包装在 try/finally 中,但这似乎有点恶心:

var expect = require('chai').expect;
var request = require('supertest');

// ...

describe('should list all tasks that the user is authorized to view', function () {

  it('the admin user should be able to see all tasks', function (done) {
    var agent = request.agent(sails.hooks.http.app);
    agent
      .post('/login')
      .send(userFixtures.admin())
      .end(function (err, res) {
        agent
          .get('/api/tasks')
          .expect(200)
          .end(function (err, res) {
            try {
              var tasks = res.body;
              expect(err).to.not.exist;
              expect(tasks).to.be.an('array');
              expect(tasks).to.have.length.of(2);
            } finally {
              done(err);
            }
          });
      });
  });
});

关于如何更好地处理这个问题的任何建议?也许Chai HTTP 会更好?

【问题讨论】:

    标签: javascript sails.js mocha.js chai supertest


    【解决方案1】:

    根据Supertest's documentation,您需要检查err 是否存在,如果存在,则将其传递给done 函数。像这样

    .end(function (err, res) {
        if (err) return done(err);
    
        // Any other response-related assertions here
        ...
    
        // Finish the test
        done();
    });
    

    【讨论】:

      【解决方案2】:

      您可以通过测试传递登录逻辑。

      // auth.js
      var request = require('supertest'),
          agent = request.agent;
      
      exports.login = function(done) {
          var loggedInAgent = agent(sails.hooks.http.app);
          loggedInAgent
              .post('/login')
              .send(userFixtures.admin())
              .end(function (err, res) {
                  loggedInAgent.saveCookies(res);
                  done(loggedInAgent);
              });
      };
      

      然后在你的测试中使用它:

      describe('should list all tasks that the user is authorized to view', function () {
      
          var admin;
      
          before(function(done) {
              // do not forget to require this auth module
              auth.login(function(loggedInAgent) {
                  admin = loggedInAgent;
                  done();
              });
          });
      
          it('the admin user should be able to see all tasks', function (done) {
      
              admin
                  .get('/api/tasks')
                  .expect(function(res) 
                      var tasks = res.body;
                      expect(tasks).to.be.an('array');
                      expect(tasks).to.have.length.of(2);
                  })
                  .end(done);
      
          });
      
          it('should have HTTP status 200', function() {
      
              admin
                  .get('/api/tasks')
                  .expect(200, done);
      
          });
      
      });
      

      使用这种方法,您不应该为每个测试登录您的管理员(您可以在您的描述块中一次又一次地重复使用您的管理员),并且您的测试用例变得更具可读性。

      您不应该使用这种方法超时,因为.end(done) 保证您的测试也将在没有错误的情况下完成。

      【讨论】:

        猜你喜欢
        • 2015-11-23
        • 2018-11-27
        • 2020-09-20
        • 1970-01-01
        • 2013-08-17
        • 2013-05-12
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        相关资源
        最近更新 更多