【问题标题】:How to set up unit tests in sailsjs如何在sailsjs中设置单元测试
【发布时间】:2015-02-05 23:14:12
【问题描述】:

我无法运行sailsjs 单元测试。似乎风帆无法扬起, 我的测试(/test/unit/test.js):

var Sails = require('sails');
var app;

before(function() {

  console.log('before');

  Sails.lift({
    log: {
      level: 'error'
    }
  }, function(err, server) {
    console.log('lifted');
    app = server;
    done(err, app);
  });

});

// Global after hook
after(function(done) {
  app.lower(done);
});

describe('mailer service', function() {

  it('should connect to gmail', function() {

    console.log(app);

  });

});

在我的应用程序文件夹中,我运行:mocha test/unit/test.js “app”变量未定义,console.log('lifted') 未被触发。我做错了什么?

【问题讨论】:

  • 如果您尝试使用mocha 命令运行所有测试,它是否正在运行?
  • @YedhuKrishnan 不,它说“0 次通过”...
  • 单元测试不应启动服务器或命中数据库/执行 i/o

标签: sails.js


【解决方案1】:

首先。 您需要使用 done 参数调用之前:

before(function(done){...})

当您使用 运行您的应用时,它是否成功提升?

sails lift

【讨论】:

  • 您好,感谢您的帮助。我运行:../node_modules/.bin/sails lift
  • 添加“完成”有助于。非常感谢!顺便说一句,我如何在我的应用程序目录中运行所有测试,只输入“mocha”?可能吗?我所有的测试都在“test”目录中。
  • 您好,很高兴能为您提供帮助。要在一个 mocha 命令中处理所有测试,您需要在测试文件夹中创建一个 mocha.opts 文件。例如,在我的上一个项目中,我有以下选项: --reporter spec --recursive --slow 300 --timeout 10000
  • 您能否解释一下,我是否需要在每次测试时提升我的应用程序?
猜你喜欢
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
相关资源
最近更新 更多