elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

Testing mounted routers which depend on Route Parameters #55

Closed jmnsf closed 7 years ago

jmnsf commented 7 years ago

First of all, thanks for the great framework, I'm having a blast learning Elixir with it! I've bumped into a limitation I think: I want to test that a nested router is correctly getting its params from the parent's route_params. An example:

defmodule Users do
  use Maru.Router

  namespace :users do
    route_param :id do
      params do
        require :id, type: Integer
      end

      namespace :posts, do: mount Posts
    end
  end
end

defmodule Posts do
  use Maru.Router

  params do
    # should be coming from the route_param
    require :id, type: Integer
  end

  get do
    # render user's posts
  end
end

In this case I'd get the path /users/:id/posts where the nested router grabs the :id param from the route. However, during unit tests of the Posts route, I've no way (afaik) of testing that it's grabbing the id from the route correctly, since all paths are relative to that router (i.e.: /).

I guess I could test this from the Users router, but that doesn't make a lot of sense since the functionality doesn't really belong to it.

I think we should be able to test this, perhaps with Routing unit tests?

falood commented 7 years ago

Thanks your feedback ❤️ I think it's a bug, I'll fix it soon

falood commented 7 years ago

I think the new unittest pr also resolved this issue, sorry for the delay.

jmnsf commented 7 years ago

Yep, that fixed it, thank you :)