title: SINON - an issue mocking a middleware
body: <p>I have the following middleware in a Node.js REST API</p>
  <pre><code>const Authorized = (req, res, next) =&gt; {
  if (!req.headers[&quot;authorization&quot;]) {
      res.status(401).send(&quot;Unauthorized&quot;);
  } else {
      jwt.verify(req.headers[&quot;authorization&quot;], PRIVATE_KEY, (err, decoded) =&gt; {
          if (err) {
              res.status(403).send(&quot;Forbidden&quot;);
          } else {
              req.businessId = decoded.businessId;
              req.roleId = decoded.roleId;
              next();
          }
      });
  }
  </code></pre>
  <p>};</p>
  <p>As you can see I'm adding to variables to the request object</p>
  <p>In the mockup of my tests I'm trying to do this:</p>
  <pre><code>sandbox = sinon.createSandbox();
      sandbox.stub(Authorized, &quot;Authorized&quot;)
          .callsFake(async (req, res, next) =&gt; {
              req.businessId = businessAux.id;
              return next();
          });
  </code></pre>
  <p>But this doesn't work and my actual function to be tested needs this variable:</p>
  <pre><code>listPermissionAndRolesByPk() {
      this.app.get(`${config.API_PATH}/permissionrolebypk`, Authorized.Authorized, async (req, res) =&gt; {
          const id = req.query.id;
          const businessId = req.businessId;
          if (!id) return res.status(400).send(messages[23].message.replace(&quot;${object}&quot;, &quot;Permission Id&quot;));
          try {
              const permission = await PermissionDAO.getPermissionAndRolesByPk(id, businessId ? businessId : 0);
              if (permission) {
                  return res.status(200).json(permission);
              } else {
                  return res.status(404).send(messages[8].message.replace(&quot;${object}&quot;, &quot;Permission&quot;));
              }
          } catch (error) {
              return res.status(error.httpCode).send(error.message);
          }
      });
  }
  </code></pre>
  <p>Any help will be appreciated.</p>