-
Notifications
You must be signed in to change notification settings - Fork 349
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
Usage of Mox (Extends #241) #355
Comments
If you use defmodule MyClient do
@adapter Application.get_env(:myapp, __MODULE__, [])[:adapter] || Tesla.Adapter.Mint
def new(...) do
# ...
Tesla.client(middlewares, adapter)
end
end as the The On the other hand, you can get quite far with static mocks. In a recent project I'm doing just that, with: defmodule FakeAdapter do
def call(%{url: "https://example.com/stuff/1"}, _) do
{:ok, json(%{"data" => "ok"})}
end
def call(%{url: "https://example.com/stuff/404"}, _) do
{:ok, json(%{"data" => "not_found"}, status: 404)}
end
end So basically use some predefined IDs to return a different response. |
Thanks for the reply! I actually managed to get a rare 2 minutes over the weekend and had a brief play with this. Looks like my knowledge of Starting out, I've been using # test_helper.exs
Mox.defmock(Tesla.MockAdapter, for: Tesla.Adapter)
# inside my test
setup do
Application.put_env(:tesla, :adapter, Tesla.MockAdapter)
end
# then inside the test cases...
# Starting with a dumb mock that matches everything
Tesla.MockAdapter
|> expect(:call, fn _env, _opts ->
{:ok, %Tesla.Env{status: 200, body: my_example_data()}}
end) And so far this has worked across multiple concurrent tests, wherein I can return different data in each test (eg. one test with good data, one returning bad data). Initially I was concerned running |
Don't put |
Yeah, this was just while I was throwing around a few things to see how it worked, but thanks for the guidance. Really appreciate your input to this issue as well as your work on |
Howdy!
I'm writing a small API wrapper and over the last couple of days, I tried to devise a pattern wherein I can reasonably test certain API calls for both happy & sad paths. I initially tried
Tesla.Mock
, but because this relies heavily on pattern matching the input request, I found it near impossible to dynamically change the response without external state.The next thing I looked at was abstracting Tesla into a generic
Http
layer and switch this out with a mock implementation in test, but it seemed overkill as I was essentially replicating the Tesla API for no benefit. Then I foundMox
, which essentially does this and #241 which starts to cover some examples.As not to pollute what is already a busy issue, I was hoping that we could start a discussion - as long as that's not inappropriate for an 'issue' - and cover some examples and possibly outline the benefits & pitfalls.
For example, I saw the examples in #241, namely
and the later note
and wasn't sure about the way to make it work for my usage, which is modules that only use
Tesla.{get,post,...}
instead of theuse Tesla
approach (if there's even a difference?).Additionally, I wasn't sure what the
MyApp.{Nice,Evil}Api.Mock
implementations would look like, or if they are simply dummies implementing@behaviour Tesla.Adapter
as the actual mocking is occurring inside of theexpect
call?The text was updated successfully, but these errors were encountered: