i trying test post
request takes in information , saves mysql database.
here router.
router.post('/dbinsert',(req,res,next)=>{ user = req.body; saveuser(user).then((result)=>{ res.send('all info saved'); }).catch((e)=>{ res.send('all info not saved'); }); });
the router works since tried post data it.
so when trying run test, says error. here test.
describe('post /dbinsert',()=>{ it('should create new user',(done)=>{ var fake = { owner : "3", firstname : "bla", lastname : "wassup", email:"unvalid@email.com", password : "akhjashjkdadh", token : "121212121" } request(app) .post('/dbinsert') .send(fake) .expect(200) .expect((res)=>{ expect(res.body).to.include(fake); }) .end((err,res)=>{ if(err) return done(err); }); }); });
here error.
1 failing 1) post /dbinsert should create new user: assertionerror: expected {} have property 'owner'
/* not related above question curious */
i new , wanted ask, how can database cloned, in not test on own database have fake database tests , gets deleted. looked @ spy, din't know if thats way it.
supertest test not behaving think. not unit test sent server. tests response server, it's not going object sent.
.expect((res)=>{ expect(res.body).to.include(fake); })
if call res.send(body)
in route, should able validate this:
.end((err, res) => { if (err) return done(err); expect(res.body).to.have.property("owner").that.equal("3"); return done(); })
another issue see you're not handling errors properly. in case, you're sending 200 in event of error, meaning don't know when fails , that's why updated validation doesn't seem work properly.
.catch((e)=>{ // difference message. res.send('all info not saved'); });
try this:
.catch((e)=>{ // now, 500 note failure res.status(500).send(e.message); });
this going timeout when successful because you're going call done
when there's error. remove if
statement.
.end((err,res)=>{ if(err) return done(err); });
this should work:
.end(done);
the whole shibang:
router.post('/dbinsert',(req,res,next)=>{ user = req.body; saveuser(user).then((result)=>{ res.send(user); next(); }).catch((e)=>{ res.status(500).send(e.message); next(); }); }); describe('post /dbinsert',()=>{ it('should create new user',(done)=>{ var fake = { owner : "3", firstname : "bla", lastname : "wassup", email:"unvalid@email.com", password : "akhjashjkdadh", token : "121212121" } request(app) .post('/dbinsert') .send(fake) .expect(200) .end((err, res) => { if (err) return done(err); expect(res.body).to.have.property('owner').that.equal(fake.owner); done(); }); }); });
to test, verified works:
const request = require('supertest'); const express = require('express'); const bodyparser = require('body-parser'); const expect = require('chai').expect; const app = express(); app.use(bodyparser.json()); app.post('/user', function(req, res) { return res.send(req.body); }); request(app) .post('/user') .send({ foo: "bar" }) .expect(200) .end(function(err, res) { if (err) throw err; expect(res.body.foo).to.equal("bar"); });
if it's still not working, there must unseen going on. try console.dir(res.body)
if doesn't work @ output.
Comments
Post a Comment