Hi All,I am looking to mock an Azure SQL database for Unit testing purposes. My database does not have, nor will have, any triggers or procedures. Its all just simple select and join statements based on CRUD requests from my express endpoint. Is it possible for me to create a fake database file containing my schema and dummy data and intercept any requests from HTTP requests?Example code I want to test inside a Users route (ie. http://localhost:3000/users )router.post('/', async (req, res) => { ...validator... try { const pool = await dbConnPoolPromise const result = await pool.request() .input('name', sql.NVarChar, validator.escape(req.body.name)) .input('role', sql.NVarChar, validator.escape(req.body.role)) .input('email', sql.NVarChar, validator.escape(req.body.email)) .query(SQL_INSERT); res.status(201); res.json(result.recordset[0]); } catch (err) { res.status(500); res.send("ERROR, Users Post" + err.message); } }); I would like to have a file that intercepts this POST request and immediately Promise resolve to, say:{recordset[{name:"Foo",role:"Bar",email:"foo@bar.com"}which shows the data that I have inserted.I can then do an assert based on this using Supertest. I have previously used Jest to mock functionality on the front end using the spyOn function so I imagine I can do something similar here but cannot find a lot of information on it. Has anyone done this before? Thanks!
Submitted July 29, 2020 at 01:09PM by rudeile
No comments:
Post a Comment