代码之家  ›  专栏  ›  技术社区  ›  LuisTensai

Meteor集成测试,使用jasmine在velocity镜像中的rest api端点

  •  2
  • LuisTensai  · 技术社区  · 10 年前

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

    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,但没有起作用。如何检查镜像的端口? 提前感谢!

    1 回复  |  直到 10 年前
        1
  •  3
  •   mc9 eclipse1203    9 年前

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

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

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

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