在远程URL上重复使用Supertest测试

问题描述 投票:11回答:2

我正在使用MochaJSSuperTest在开发过程中测试我的API,并且绝对喜欢它。

但是,在将代码发布到生产环境之前,我也想通过同样的测试对登台服务器进行远程测试。

是否可以通过远程URL或远程URL的代理提供请求?

这里是我使用的测试样本

        request(app)
        .get('/api/photo/' + photo._id)
        .set(apiKeyName, apiKey)
        .end(function(err, res) {
            if (err) throw err;
            if (res.body._id !== photo._id) throw Error('No _id found');
            done();
        });
node.js mocha supertest
2个回答
17
投票

我不确定您是否可以通过超级测试来做到这一点。您绝对可以使用superagent完成此操作。超级测试基于超级代理。一个例子是:

var request = require('superagent');
var should = require('should');

var agent = request.agent();
var host = 'http://www.yourdomain.com'

describe('GET /', function() {
  it('should render the index page', function(done) {
    agent
      .get(host + '/')
      .end(function(err, res) {
        should.not.exist(err);
        res.should.have.status(200);
        done();
      })
  })
})

因此,您不能直接使用现有测试。但是它们非常相似。如果您添加

var app = require('../app.js');

在测试的顶部,您可以通过更改host变量轻松地在测试本地应用程序和远程服务器上的部署之间切换>

var host = 'http://localhost:3000';

编辑:

刚刚在docs#example中找到了超级测试的示例

request = request.bind(request, 'http://localhost:5555');  // add your url here

request.get('/').expect(200, function(err){
  console.log(err);
});

request.get('/').expect('heya', function(err){
  console.log(err);
});

0
投票

您已经提到了它,因为您要定位远程URL,所以只需将应用替换为远程服务器URL

© www.soinside.com 2019 - 2024. All rights reserved.