Git Product home page Git Product logo

Comments (4)

cschilbe avatar cschilbe commented on May 27, 2024 1

When the external request is not the thing you are testing, you may be interested in using the stub/4 function. It was added specifically for this case.

Added here #58

Bypass.stub(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
  Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
  Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
end)

from bypass.

Odaeus avatar Odaeus commented on May 27, 2024

This would be great! I have this problem when testing async work triggered by AMQP messages. My current workaround for this goes like:

test "something" do
  test = self()
  Bypass.expect(bypass, "POST", "/endpoint", fn conn ->
    send(test, :request_done)
    # ...response
  end)

  trigger_work()

  # Wait for the message
  assert_received :request_done, :timer.seconds(3)
end

In case that helps anyone.

EDIT: This technique isn't reliable in all circumstances because the test can still end before the process finishes 😕. Which in my cases leads to a failure as the test is supervising and shuts down the process abnormally. send_after could be used as a band-aid.

from bypass.

ream88 avatar ream88 commented on May 27, 2024

I just tripped exactly over the same stuff and was looking through the docs on how to solve this.
@danielemegna happy to help if you want to takle this issue!

from bypass.

mauricius avatar mauricius commented on May 27, 2024

@Odaeus I use the same technique, which is actually accepted in the community, cosidering that it's also suggested in the documentation of the Mox library.

In my case I usually do the following:

test "something" do
  parent = self()
  ref = make_ref()

  Bypass.expect(bypass, "POST", "/endpoint", fn conn ->
    # ...response
    send(parent, {ref, :sent})
  end)

  trigger_work()

  assert_receive {^ref, :sent}
end

from bypass.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.