2013-02-19 28 views
0

我很新的摩卡/ omf。我有以下的基本测试:如何获得响应的身体与摩卡/ node.js

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

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

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

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

**更新**

我试着摩卡测试,只是一个简单的状态代码:

request = require("request"); 

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

,但我得到了以下错误:

TypeError: Object # has no method 'expect'

任何想法?摩卡需要额外的插件吗?

+0

有你使用supertest考虑?这工作得很好。 – AndyD 2013-02-19 14:16:12

+0

听起来很酷。它似乎能够匹配解析的身体对象...你有任何例子吗? – Luc 2013-02-19 15:44:50

回答

4

第二个示例无法如图所示工作。 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(); 
    }) 
    }); 
}); 
+0

正是我在找的东西。谢谢 ! – Luc 2013-02-20 09:42:42