All the examples of “Plug test” I see make actual requests with paths all the way from Endpoint to actions. So it tests the whole thing and changing other things would affect the test without changing the module itself.
I’d like to make contained tests only for Plugs. I’d need conn after Endpoint but before Router. It might look like
test "plugs App.MyPlug", %{conn: conn} do
conn = conn
|> plug(App.Endpoint)
|> plug(App.MyPlug)
assert conn == some_desired_state
end
Can I actually do this with setup?
Also, is this approach something not feasible or unnecessary? I thought this is closer to normal function tests.
I don’t personally test plugs, just test that they have their desired effect as a side-effect of other areas they affect.
If you want to test in isolation though (which is perfectly reasonable) there is no need to do it as a “ConnCase”—that is actually testing way too much. You should be able to manually up a %Plug.Conn{} with the fields you care about, pass it in, then assert the resulting conn has your expected changes.