Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Reverse the order of the routes
Browse files Browse the repository at this point in the history
why? because reasons!

by default, express route definitions are "first-in wins".
that means when you're building an app you want to start with
specific routes and then declare increasingly generic routes.

however, test stubs typically want to be declared in the reverse
order. that is to say, a general stubbing is made at the top
of an example group so that it can cascade downward (to nested
example groups), whereas nested example groups typically want
to declare more specific routes to facilitate the specific
needs of the test.
  • Loading branch information
searls committed Sep 10, 2013
1 parent 656cb3e commit f5dd5df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/covet.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports =
else
res.send(400)

stubbedRoutes.push(_(app.routes[stubbing.verb]).last())
newRoute = _(app.routes[stubbing.verb]).last()
app.routes[stubbing.verb] = [newRoute].concat(_(app.routes[stubbing.verb]).initial())
stubbedRoutes.push(newRoute)
res.send(201)

app.delete config.routes.resetRoutes, (req, res) ->
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/covet_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,24 @@ describe 'covet', ->
expect(res.statusCode).to.equal(400)
done()

describe "overriding routes", ->
beforeEach (done) ->
post 'covet/routes',
verb: 'get'
path: '/bunnies/:id'
response:
json:
name: "Default Bunny"
, ->
post 'covet/routes',
verb: 'get'
path: '/bunnies/3'
response:
json: BUNNY
, ->
done()

it 'gets the more-specifically-stubbed bunny', (done) ->
get "bunnies/3", (body) ->
expect(body).to.deep.equal(BUNNY)
done()

0 comments on commit f5dd5df

Please sign in to comment.