bypass icon indicating copy to clipboard operation
bypass copied to clipboard

Support expectation on exact number of requests

Open RudolfMan opened this issue 2 years ago • 1 comments

Hi there!

Sometimes I want to set an expectation that my app will call particular endpoint an exact number of times.. I figured I can achieve that by setting some counter and increment it on each request, then assert on its value.. though, I think it would be nice to be able to set that expectation via Bypass.expect functions.. something similar to Mox.expect/4

So in this change I attempted to do exactly that:

Bypass.expect(bypass, 3, fn conn ->
  # ...
  # Plug.Conn.send_resp(conn, 200, "")
end)

and

Bypass.expect(bypass, "GET", "/foo", 3, fn conn ->
  # ...
  # Plug.Conn.send_resp(conn, 200, "")
end)

And if that number of requests is not met - it will raise an error:

     Expected 3 HTTP request for Bypass at GET /foo, got 5
     stacktrace:
       (bypass 2.1.0) lib/bypass.ex:122: Bypass.do_verify_expectations/2
       ...

RudolfMan avatar Apr 03 '23 10:04 RudolfMan

@bszaf, thank you for the review. I added docs in https://github.com/PSPDFKit-labs/bypass/pull/128/commits/7609e767f215915c3f5e9a4ac09377ba105c302a

RudolfMan avatar Jun 27 '23 17:06 RudolfMan