Search code examples
node.jsjunitjasminekarma-jasminejasmine-node

Jasmine spyOn function argument object, as Variables are not defined


I am wiring jasmine test cases for one of the application. I have just started learning jasmine. Below is my script code

var aclChecker = function(app,config) {

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

}

Now i want to spyOn res and req object to know if send method has been called. As req and res are not global variables i am having this doubt on how to create a spy on in junit spec

Please help!!!!!!!!


Solution

  • You can simply mock req and res like this.

      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');
        });
      });