wojtekmach/req

`Req.Test.expect`: Order

wojtekmach opened this issue · 2 comments

In Mox, order matters:

Mix.install([:mox])

defmodule Foo do
  @callback upcase(String.t()) :: String.t()
end

Mox.defmock(Foo.Mock, for: Foo)

ExUnit.start()

defmodule Test do
  use ExUnit.Case
  import Mox
  setup :verify_on_exit!

  test "order matters" do
    expect(Foo.Mock, :upcase, fn "foo" -> "FOO" end)
    expect(Foo.Mock, :upcase, fn "bar" -> "BAR" end)

    Foo.Mock.upcase("foo")
    Foo.Mock.upcase("bar")

    expect(Foo.Mock, :upcase, fn "foo" -> "FOO" end)
    expect(Foo.Mock, :upcase, fn "bar" -> "BAR" end)

    # this line raises, the order matters, that's good!
    Foo.Mock.upcase("bar")

    Foo.Mock.upcase("foo")
  end
end

but in Req.Test, it currently is not preserved:

Mix.install([
  {:req, github: "wojtekmach/req"},
  :plug
])

ExUnit.start()

defmodule Test do
  use ExUnit.Case

  test "order matters" do
    Req.Test.expect(:foo, fn conn ->
      assert conn.method == "PUT"
      Plug.Conn.send_resp(conn, 200, "")
    end)

    Req.Test.expect(:foo, fn conn ->
      assert conn.method == "GET"
      Plug.Conn.send_resp(conn, 200, "bar")
    end)

    # this line raises, expected conn.method to be PUT but it is GET
    assert Req.put!(plug: {Req.Test, :foo}, body: "bar").body == ""

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

should we preserve it?

cc @whatyouhide

Oh man what?! Didn't I store expectations in a list?

In any case yes 100% we should keep the order, that's like half of the point 🤦 I can work on this next week, I’m on vacation with no laptop this week.

No rush!