我正在尝试为用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/