javascript - Meteor 集成测试,使用 jasmine 在 Velocity 镜像中休息 api 端点

标签 javascript rest meteor meteor-velocity meteor-jasmine

我正在尝试为用meteor编写的API端点创建一个测试。我正在使用 Jasmine 和速度。它旨在在同一个项目中运行,这就是我使用它们的原因。 当我尝试运行测试并检查端点中的数据时,问题就出现了。我在 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
  }
);

当测试运行时,您的测试镜像将动态生成绝对网址。

关于javascript - Meteor 集成测试,使用 jasmine 在 Velocity 镜像中休息 api 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373851/

相关文章:

reactjs - 在客户端 React 组件中导入 npm 模块

javascript - meteor .js : Execute method after list has been populated

javascript - 如何在我的网站上禁用或隐藏不需要的 Disqus 广告?

rest - 升级到 Grails 2.3.0 时缺少 RESTful 请求的参数

database - 是否有轻量级、可嵌入的键/值数据库? (类似于 diet couchdb)

java - Curl 在 JetBrains IDE (Windows) 中无法用于 POST

php - 在 php 服务器端使用 meteor.js

c# - JavaScript window.open 有时返回 null

javascript - 点击 li 时,jQuery 的 .blur() 不会被触发

javascript - 根据可观察到的变化刷新 ViewModel