I've been reading about the various schools of thought wrt testing Elixir code , ie, mocking vs dependency injection vs module swapping using configuration.
I'm personally in favor of injecting dependencies to functions. Now, I am not sure if core libraries like Plug etc have been built around this idea.
Let's say I have a router that looks like this :
```elixir
defmodule MyRouter do
use Plug.Router
plug :match
plug :dispatch
post "/attack" do
# missile_launcher.launch_missile -> can the module "missile_launcher" be injected ?
send_resp(conn, 200, "Missile launched")
end
match _ do
send_resp(conn, 404, "oops")
end
end
```
How do I inject a module into the router so that I can use it inside the actions (see the comment above).
This would make it easy to test this route using a mock module:
```elixir
defmodule MyRouterTest do
use ExUnit.Case
use Plug.Test
# Can I use a fake MissileLauncher module when initializing the router ?
@opts MyRouter.init([])
test "asks Missile module to launch a missile" do
conn = conn(:post, "/attack")
conn = MyRouter.call(conn, @opts)
assert conn.status == 200
assert conn.resp_body == "Missile launched"
end
end
```
Does the Plug.Router's init function allow us to pass in dependencies (or any custom options)
that can be accessed inside the route matchers ?
Cheers,
Emil