Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing requests #195

Closed
wilbertom opened this issue Mar 24, 2018 · 3 comments
Closed

Testing requests #195

wilbertom opened this issue Mar 24, 2018 · 3 comments

Comments

@wilbertom
Copy link

Hello, I'm looking for a way to test against the request sent by a module. My initial intuition was to capture the request during the mock but I'm having trouble doing it in a clean way.

defmodule ApiTest do      
  use ExUnit.Case                  

  setup do                     
    Tesla.Mock.mock fn             
      %{method: :get} = env ->     
        # capture request env here              
        %Tesla.Env{}                         
    end          

    {:ok}        
  end            

  test "issue some request here" do                   
    Api.get("")           
    
    assert request.query == [query: "hello world", token: "12345"]
  end            
end

My second intuition is to create a middleware that pushes the untouched request at the beginning of the pipeline into somewhere I can read from in the test.

I tried using a global variable but couldn't figure out how to do that in elixir but, I didn't spend too much time toying with that idea because then tests would have to always run serially.

@teamon
Copy link
Member

teamon commented Mar 24, 2018 via email

@teamon
Copy link
Member

teamon commented Mar 24, 2018

In this case you don’t even need to register the process since Tesla mock runs in the same process so just

# in mock
send self(), env

# in test
assert_receive ...

@wilbertom
Copy link
Author

Thank you very much @teamon, I went with sending a message to self.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants