如何通过mocha / node.js获取响应的主体

Luc*_*Luc 1 mocha.js node.js

我对摩卡/ OMF非常陌生。我有以下基本测试:

omf('http://localhost:7000', function(client) {
  client.get('/apps', function(response){
    response.has.statusCode(200);
    response.has.body('["test1","test2"]');
  });
});
Run Code Online (Sandbox Code Playgroud)

我想检查值“ test2”是否在返回的列表中,但我无法弄清楚这是如何可行的。我在想类似的东西:

omf('http://localhost:7000', function(client) {
  client.get('/apps', function(response){
    response.has.statusCode(200);
    // response.body.split.contains("test2"); // Something like that
  });
});
Run Code Online (Sandbox Code Playgroud)

我可以访问response.body然后解析字符串吗?

**更新**

我尝试用mocha进行测试,只是一个简单的状态代码:

request = require("request");

describe('Applications API', function(){
  it('Checks existence of test application', function(done){
    request
      .get('http://localhost:7000/apps')
      .expect(200, done);
  });
});
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

TypeError:对象#没有方法“期望”

任何的想法 ?摩卡咖啡是否需要其他插件?

Pas*_*cle 6

第二个示例不能如图所示工作。request.get是异步的。

这是一个运行请求并应运行的示例

request = require("request");
should = require("should");

describe('Applications API', function() {
  it('Checks existence of test application', function(done) {
    request.get('http://google.com', function(err, response, body) {
      response.statusCode.should.equal(200);
      body.should.include("I'm Feeling Lucky");
      done();
    })
  });
});
Run Code Online (Sandbox Code Playgroud)