我是茉莉花的新手,并尝试进行基本测试但是我一直收到错误。
我的测试如下
var request = require('request');
var base_url = 'http://localhost:8080'
var getdata = require('../modules/getdata.js')
var index = require('../index.js')
describe("GetData Test",function(){
describe("Get /", function() {
it("returns statusCode 200", function(done) {
request.get(base_url, function(error, response, body){
expect(response.statusCode).toBe(200);
done();
});
});
it("returns Api is Online", function(done) {
request.get('base_url', function(error, response, body){
console.log(request.body)
expect(body).toBe("Api is online");
index.closeServer();
done();
});
});
});
});
此代码正在测试以下函数
server.get('/', getdata.test)
和getdata.test如下
exports.test = function test(req, res) {
res.send('Api is online')
};
第一个测试通过,因为responseCode确实是200.但是第二个测试错误显然是身体未定义。有帮助吗?错误如下:
jack@JacksLaptop:~/workspace$ npm test
> jack_tidbury_web_api@1.0.0 test /home/jack/workspace
> jasmine-node spec
App running on port 8080
.undefined
F
Failures:
1) GetData Test Get / returns Api is Online
Message:
Expected undefined to be 'Api is online'.
Stacktrace:
Error: Expected undefined to be 'Api is online'.
at Request._callback (/home/jack/workspace/spec/getdata_spec.js:17:22)
at self.callback (/home/jack/workspace/node_modules/request/request.js:186:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.init (/home/jack/workspace/node_modules/request/request.js:274:17)
at new Request (/home/jack/workspace/node_modules/request/request.js:128:8)
at request (/home/jack/workspace/node_modules/request/index.js:54:10)
at Function.get (/home/jack/workspace/node_modules/request/index.js:62:12)
Finished in 0.051 seconds
2 tests, 2 assertions, 1 failure, 0 skipped
npm ERR! Test failed. See above for more details.
答案 0 :(得分:1)
您的意思是回复body
而不是request.body
。此外,您将获得'base_url'
而不是base_url
。试试这个:
it("returns Api is Online", function(done) {
// Here you were getting 'base_url', which threw an error because that's not a valid url
request.get(base_url, function(error, response, body){
console.log(body); // Here you were logging request.body
expect(body).toBe("Api is online");
index.closeServer();
done();
});
});