简体   繁体   English

流星集成测试,在茉莉花中将api终结点放在速度镜中

[英]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. 我正在尝试为用流星编写的API端点创建测试。 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. 我在mongodb副本中有一个引导数据集,当我发布它时,它与本地应用程序中引导的数据集不匹配。 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. 我认为它应该指向镜子的其余API。 Is there a way to do that? 有没有办法做到这一点? I changed localhost:3000 to localhost:5000 and it didn't work. 我将localhost:3000更改为localhost:5000,但没有成功。 How can I check the mirror's port? 如何检查镜子的端口? Thanks in advance! 提前致谢!

Use Meteor.absoluteUrl rather than hard coding the port. 使用Meteor.absoluteUrl而不是对端口进行硬编码。

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. 测试运行时,您的测试镜像将动态生成一个绝对URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM