Jack Tidbury Jack Tidbury - 11 days ago 7
Node.js Question

Jasmine-node Body is undefined

I am new to jasmine and attempting to run a basic test however I keep getting an error.

My testing is as follows

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();
});
});
});
});


This code is testing the following Functions

server.get('/', getdata.test)


and getdata.test is the following

exports.test = function test(req, res) {
res.send('Api is online')
};


The first test passes, as the responseCode is indeed 200. However the second tests errors as apparently the body is undefined. Any help? The error is as follows:

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.

Answer

You meant response body instead of request.body. Also, you are getting 'base_url' instead of base_url. Try this:

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();
  });
});