【问题标题】:Jasmine spyOn function argument object, as Variables are not definedJasmine spyOn 函数参数对象,因为未定义变量
【发布时间】:2014-12-21 23:52:45
【问题描述】:

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

var aclChecker = function(app,config) {

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

}

现在我想监视 resreq 对象,以了解是否调用了 send 方法。 由于 req 和 res 不是全局变量,我对如何在 junit 规范中创建间谍有疑问

请帮忙!!!!!!!!!

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    查看此内容以获取有关 beforeEach 的更多帮助: https://github.com/pivotal/jasmine/wiki/Before-and-After

    【讨论】:

      【解决方案2】:

      您可以像这样简单地模拟 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');
          });
        });
      

      【讨论】:

        猜你喜欢
        • 2016-05-12
        • 1970-01-01
        • 2016-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-23
        相关资源
        最近更新 更多