node.js - Using supertest to check didFlash after a redirect -
i'm trying test happens when user destroy callback receives error user controller. when destroy receives error, following:
flash('error', 'can not destroy user'); redirect(path_to.users);
this test far:
it('should fail on delete /users/:id if destroy receives error', function (done) { var user = app.models.user; var user = new userstub(); user.find = sinon.spy(function (id, callback) { callback(null, user); }); user.destroy = sinon.spy(function (callback) { callback(new error()); }); request(app) .del('/users/55') .end(function (err, res) { res.header.location.should.include('/users'); app.didflash('error').should.be.true; done(); }); });
i've seen this question , res.header..
portion works expected. however, i'm still confused on how can test flash happens after redirect.
i ended changing users_controller use following code destroy callback (the redirect having other issues):
if (error) { flash('error', 'can not destroy user'); } else { flash('info', 'user removed'); } send(302, "'" + pathto.users + "'");
the init.js file used mocha.js has few pieces in when initializing app object (some irrelevant code omitted):
global.getapp = function(done) { var app = require('compound').createserver(); app.renderedviews = []; app.flashedmessages = {}; app._render = app.render; app.render = function (viewname, opts, fn) { app.renderedviews.push(viewname); // deep-copy flash messages var flashes = opts.request.session.flash; for(var type in flashes) { app.flashedmessages[type] = []; for(var in flashes[type]) { app.flashedmessages[type].push(flashes[type][i]); } } return app._render.apply(this, arguments); }; app.use(function (req, res, next) { app._request = req; next(); }); app.didflash = function (type) { var flashes = app._request.session.flash; return !!(app.flashedmessages[type] || (flashes && flashes[type])); }; return app; };
the original way of checking didflash
limited rendering, checks if flash message created before redirect
or send
.
Comments
Post a Comment