Meteor集成测试,在velocity'她的镜子上有茉莉花

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

本文关键字:镜子 茉莉花 集成测试 velocity Meteor      更新时间:2023-09-26

我正在尝试为使用meter编写的API端点创建一个测试。我用的是茉莉花和速度。它打算在同一个项目中运行,这就是我使用它们的原因。当我试图运行测试并检查端点中的数据时,问题就出现了。我在mongodb副本中有一个引导的数据集,当我发布它时,它与本地应用程序中引导的数据不匹配。下面是示例代码:

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

我认为它应该指向镜像的rest api。有办法做到这一点吗?我将localhost:3000更改为localhost:5000,但它不起作用。如何检查镜像的端口?提前感谢!

使用Meteor.absoluteUrl而不是硬编码端口。

在你的代码中,这样做:

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

当测试运行时,您的测试镜像将动态生成一个绝对url。