Skip to content

Commit

Permalink
Req.Test: Allow plug stub to be a module or {module, options}
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Mar 12, 2024
1 parent 4f2fe61 commit af28ef5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
22 changes: 21 additions & 1 deletion lib/req/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ defmodule Req.Test do

@doc false
def call(conn, stub_name) do
stub(stub_name).(conn)
case stub(stub_name) do
fun when is_function(fun) ->
fun.(conn)

module when is_atom(module) ->
module.call(conn, module.init([]))

{module, options} when is_atom(module) ->
module.call(conn, module.init(options))

other ->
raise """
expected stub to be one of:
* 0-arity function
* module
* {module, options}
got: #{inspect(other)}\
"""
end
end
end
23 changes: 18 additions & 5 deletions test/req/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ defmodule Req.TestTest do
assert Req.Test.stub(:foo) == 2
end

test "plug" do
Req.Test.stub(:foo, fn conn ->
Plug.Conn.send_resp(conn, 200, "hi")
end)
describe "plug" do
test "function" do
Req.Test.stub(:foo, fn conn ->
Plug.Conn.send_resp(conn, 200, "hi")
end)

assert Req.get!(plug: {Req.Test, :foo}).body == "hi"
end

assert Req.get!(plug: {Req.Test, :foo}).body == "hi"
test "module" do
defmodule Foo do
def init(options), do: options
def call(conn, []), do: Plug.Conn.send_resp(conn, 200, "hi")
end

Req.Test.stub(:foo, Foo)

assert Req.get!(plug: {Req.Test, :foo}).body == "hi"
end
end

describe "allow/3" do
Expand Down

0 comments on commit af28ef5

Please sign in to comment.