I am trying to test two routes that are almost the same, except that one of them is more specific, since the last segment of the URL is a hard-coded value (edit), while the other has a parameter (: slug).
The problem that I encountered is that when the request is executed, it will call both routes (edit, show), which is why my mock never () wait will never pass: (
Am I doing something wrong? I donβt understand why both routes run if one of them is more specific ...
This is a test:
var request = require('supertest') , express = require('express') describe('routes', function() { it('should call only edit', function(done) { var usersController = require('./users-controller'); var sandbox = require('sinon').sandbox.create(); var mockController = sandbox.mock(usersController); mockController.expects('edit').yields(null).once(); mockController.expects('show').never(); var app = express(); app.get('/users/:id/edit', usersController.edit); app.get('/users/:id/:slug', usersController.show); request(app) .get('/users/123/edit') .end(function(err, res){ if (err) throw err; mockController.verify(); done(); }); }); });
and here is users-controlle.js I make fun of above:
exports.edit = function(req, res, next) { res.send('edit'); } exports.show = function(req, res, next) { res.send('show'); }
source share