Jasmine spyOn函数参数对象,因为Variables没有定义

时间:2014-10-26 19:06:40

标签: node.js junit jasmine karma-jasmine jasmine-node

我正在为其中一个应用程序连接茉莉花测试用例。我刚开始学习茉莉花。 以下是我的脚本代码

var aclChecker = function(app,config) {

    validateResourceAccess = function (req, res, next) {
            req.callanotherMethod();
            res.send(401,'this is aerror message');
   }

}

现在我想窥探 res req对象,知道是否已调用send方法。 由于req和res不是全局变量,我对如何在junit规范中创建间谍有疑问

请帮助!!!!!!!!

2 个答案:

答案 0 :(得分:3)

您可以像这样简单地模仿reqres

  describe("acl checker", function() {
    it("should validate resource access", function() {
      var mockReq = {
          callAnotherMethod: function() {}
      };
      var mockRes = {
        send: function() {}
      };
      var mockNext = {};
      spyOn(mockReq, "callAnotherMethod");
      spyOn(mockRes, "send");

      aclChecker.validateResourceAccess(mockReq, mockRes, mockNext);
      expect(req.callAnotherMethod).toHaveBeenCalled();
      expect(res.send).toHaveBeenCalledWith(401, 'this is aerror message');
    });
  });

答案 1 :(得分:1)

通常,在单元测试中,您将模拟任何资源请求,并仅验证请求是否正确。因此,您将调用模拟请求库,并验证URL和标头是否正确。

但是,如果您确实想要测试对资源的实际访问权限,则需要首先构建您的请求对象,然后让自己访问它。

如果您想了解请求模拟,请查看jasmine-ajax: https://github.com/pivotal/jasmine-ajax

如果您仍想这样做,则应在测试文件中使用beforeEach函数来创建测试所需的依赖项。

看看这个有关beforeEach的更多帮助: https://github.com/pivotal/jasmine/wiki/Before-and-After

相关问题