【问题标题】:Start locomotive.js 0.4.x for functional testing启动 locomotive.js 0.4.x 进行功能测试
【发布时间】:2014-05-02 03:13:28
【问题描述】:

The way to start locomotive.js 0.3.x 不再适用于 0.4.x,因为 app.boot 的签名不同。我有:

before(function(done) {
  var self = this;
  var lcm = new locomotive.Locomotive();
  lcm.boot('test', function() {
    self.app = lcm.express;
    self.app.listen(4000, done);
  });
});

当我尝试与 supertest 连接时,它会抛出“错误:连接 ECONNREFUSED”:

  request(this.app)
    .post('problems/' + problemId + '/solution_ratings')
    .set('Content-Type', 'application/json')
    .send({access_token: playerId, solution_group_id: 1, rate: 4})
    .expect(200, done);

启动机车服务器进行功能测试的正确方法是什么?

[更新] 我必须在与测试相同的进程中启动服务器,以便使用 sinon.js 对模型的调用进行 stub/spy 调用。

【问题讨论】:

    标签: functional-testing locomotivejs


    【解决方案1】:

    我已经在 Locomotive 的 github 上回答了这个问题,但我想在这里分享以获得一些反馈,例如以更好的方式、更清洁的方式或您可能有的任何其他意见。

    我需要 OAuth2 环境中的多个服务器(身份验证、资源、仪表板..),其中大部分是基于 Locomotive 框架的。

    我喜欢功能测试,因为它们最能代表基于 OAuth2 的身份验证,而不是仅将我的测试集中在身份验证令牌可能无法最好地代表用户的资源服务上。

    这是我为启动机车服务器而设计的设置:

    对于您的测试,请说test/util.severs.js

    var fork = require('child_process').fork,
        async = require("async");
    
    var authApp;
    var serverStatus = [];
    
    var start = function(done) {
    
        authApp = fork( __dirname + '/../../authorization/server.js');
        serverStatus.push(function(callback){
            authApp.on('message', function(m){
                //console.log('[AUTHORIZATION] SENT MESSAGE: ', m);
                if (m.status == 'listening') return callback();
            });
        });
    
       // add others servers if you swing that way!
    
       async.parallel(serverStatus, function(){
             done();
       });
    }
    var stop = function(done) {
        authApp.kill();
        done();
    }
    module.exports = {
      start: start,
      stop: stop
    };
    

    请注意,我在这里使用异步,因为我正在使用多台服务器(环境在 OAuth2 中,需要许多服务器才能启动(IE 资源、仪表板...)),如果您将拥有所有内容,请忽略异步是一台服务器。

    在您的 Mocha 测试中需要上述文件并执行

    servers = require('./util/servers');
    ...
    before(servers.start);
    
      // tests away!!!
    
    after(servers.stop);
    

    然后在我的每个 locomotive-project/server.js 我执行以下操作...

    // if being started as a parent process, send() won't exist, simply define
    //   it as console.log or what ever other thing you do with logging stuff.
    if (process.send === undefined){
      process.send = function(msg){
        console.log("status: ", msg.status);
      };
    }
    process.send({status: 'starting'});
    ...
    app.boot(function(err) {
      if (err) {
        process.send({status: 'error', message: err});
        return process.exit(-1);
      } else {
        // notify any parents (fork()) that we are ready to start processing requests
        process.send({status: 'listening'});
      }
    });
    

    在此处使用 Locomotive 0.4.x,根据您的版本可能会有所不同。

    这是最好的方法吗?理想情况下,我想将其转移到 Grunt,即测试服务器初始化(这是相当庞大的,因为我们将大量数据构建到测试数据库中),然后可以运行功能测试。所以想知道是否有人研究过 grunt 来代替 Mocha 中的 before() 任务。

    【讨论】:

    • app.boot 似乎没有启动服务器,假设app 是由new locomotive.Locomotive() 创建的
    • 是的,它是这样创建的,也许你可以显示一些代码,我在最后导出它:exports = module.exports = app; 但这是来自之前的测试尝试......让我检查它是否仍然必填。
    • 不,你不需要在你的 loco-proj/server.js 中导出,所以我对你的评论有点困惑,你是说它不起作用,还是你不是'不确定 app.boot() 是否启动。只是好奇,因为我想了解更多自己。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多