Mutiple stubs in one test

Is it possible to stub multiple times within a test using req?

I am doing

Req.Test.stub(Wrapper, &Req.Test.json(&1, %{"indices" => %{}}))
Req.Test.stub(Wrapper, &Req.Test.json(&1, %{"acknowledged" => true}))

but it’s only picking up the very last stub where as in module, there are multiple HTTP calls let say 5 and every call expecting something.

Is it possible to stub it in a way that it can justify each request?

For that, use Req.Test.expect.

with that it says

** (RuntimeError) no mock or stub for Wrapper

Could you create a minimal reproduction?

You can augment this script:

Mix.install([:req, :plug])

Req.Test.expect(Wrapper, &Req.Test.json(&1, %{a: 1}))
Req.Test.expect(Wrapper, &Req.Test.json(&1, %{a: 2}))

%{"a" => 1} = Req.get!(plug: {Req.Test, Wrapper}).body
%{"a" => 2} = Req.get!(plug: {Req.Test, Wrapper}).body
2 Likes