【问题标题】:Meteor integration testing, rest api endpoint in velocity's mirror with jasmineMeteor 集成测试,在 Velocity 的镜像中使用 jasmine 休息 api 端点
【发布时间】:2015-06-05 02:24:08
【问题描述】:

我正在尝试为使用流星编写的 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 并且它不起作用。如何查看镜像的端口? 提前致谢!

【问题讨论】:

  • 似乎端口选项现在是自动的,而不是硬编码为 5000。您可以使用 JASMINE_SERVER_MIRROR_PORT 环境变量配置端口(在某些地方命名不好)。想知道是否有办法获取镜像在运行时运行测试的 ip 和端口。
  • 如果在Meteor.http.call中使用相对路径会怎样?

标签: javascript rest meteor meteor-velocity meteor-jasmine


【解决方案1】:

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

在您的代码中,执行以下操作:

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

测试运行时,你的测试镜像会动态生成一个绝对url。

【讨论】:

  • 谢谢!改变了我的测试,它奏效了!很遗憾我不能投票!
  • 这在当前 Meteor 版本中有效吗? 1.2.1
猜你喜欢
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
  • 2013-12-16
  • 1970-01-01
  • 2015-07-06
相关资源
最近更新 更多