Supernatural and repeated execution of such routes

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'); } 
+4
source share
2 answers

What to expect.

Express will execute all routes matching this URL in the order in which they were registered with the app object. To get around any subsequent callbacks, you can use next('route') . For more information, see the Application Routing Section in the Course API .

0
source

Try putting your "show" in a callback in a function called "edit" done

0
source

All Articles