javascriptrestmeteormeteor-velocitymeteor-jasmine

Meteor integration testing, rest api endpoint in velocity's mirror with jasmine


I'm trying to create a test for an API endpoint written with meteor. I'm using jasmine and velocity. It's intended to run within the same project, that's why I'm using them. The problem comes when I'm trying to run the test and check for data in the endpoint. I have a bootstraped dataset in the mongodb replica, and when I POST it, it doesn't match the one that's bootstrapped in the local app. Here's the example code:

Jasmine.onTest(function () {

describe('RestApi.MyMethod', function () {

it('Expects to fail because it lacks of valid parameters', function () { /*but it fails because of the user can't be found in the real app*/
  var response = "";
  var userId = Meteor.users.findOne({"username": "MyUser"})._id;
  try {
    response = Meteor.http.call(
      "POST",
      "http://localhost:3000/api/myMethod",
      {
        data: {
          "userId": 
        },
        timeout: 1000
      }
    );
  } catch(error){
    expect(error.message.indexOf("failed [400]")).toBeGreaterThan(-1);
    expect(error.message.indexOf("Invalid parameters provided")).toBeGreaterThan(-1);
  }

  expect(response).toBe('');

});

});

});

I think it should point to the mirror's rest api. Is there a way to do that? I changed localhost:3000 to localhost:5000 and it didn't work. How can I check the mirror's port? Thanks in advance!


Solution

  • Use Meteor.absoluteUrl rather than hard coding the port.

    In your code, do this:

    response = Meteor.http.call(
      "POST",
      Meteor.absoluteUrl("api/myMethod"), // this bit has changed.
      {
        data: {
          "userId": 
        },
        timeout: 1000
      }
    );
    

    When the test runs, your test mirror will dynamically generate an absolute url.