title: Node js express js API test case with fake request and response data
body: <p>I am working on write node js express js API test case using jasmine but I have one problem with my test case it's only working with live database connection and its return actual value from the database so in future may be data will change so at that time test case will fail.</p>
  
  <p>So I want to create a mock of my controller and database query model</p>
  
  <p>Here is my controller code </p>
  
  <pre><code>module.exports.updateProductStatus = async (req, res, next) =&gt; {
      let result = await apiModel.updateProductStatus(req.body);
      if (result.affectedRows &gt; 0) {
          res.send({
              status: "success",
              message: "product status updated."
          })
      }
      else {
          res.send({
              status: "error",
              message: "failed to update product status."
          })
      }
  };
  </code></pre>
  
  <p>and here is my MySQL database query module</p>
  
  <pre><code>module.exports.updateProductStatus = async (data) =&gt; {
      let updateData = {
          STATUS : data.status,
          UPDATED_TIMESTAMP : moment().unix()
      }
      let result = await db.query('UPDATE products SET ? WHERE PRODUCT_ID = ?',[updateData,data.product_id]).catch(handleError);
      return result[0];
  };
  </code></pre>
  
  <p>Here is my test case to call API and send request data </p>
  
  <pre><code>it("should update product status", () =&gt; {
              var reqData = {
                  "account_name" : "demo",
                  "last_update": 1571298222,
                  "product_detail": {
                      "QUENTITY":"36","PRICE":"5.69527","TITLE":"AAAAAAAAA","SKU":"A10512","UPC":""
                  },
                  "sku": "A10512AAA",
                  "status": "inactive",
                  "product_id": 1
              };
              request.post(
                  {
                      url: 'localhost:9090/api/update-product-status',
                      headers: {
                          'authorization': 'Bearer eyJhbGciOiJIUzI1NiddIsInR5cCIdd'
                      },
                      form: reqData
                  }, function (err, httpResponse, body) {
                      console.log("TCL: err", err);
                      console.log("TCL: body", body);
                      console.log("TCL: httpResponse", httpResponse);
                  });
          });
  </code></pre>
  
  <p>please guide me how to get a response without connecting MySQL database when run test case, if there is an NPM library for mock, let me know event better if you provide sample code </p>
  
  <p>thanks </p>