Skip to content

Commit

Permalink
chore: improve how-to test with mox docs
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Oct 24, 2024
1 parent fe7207c commit ca26020
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions guides/howtos/test-using-mox.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Test Using Mox with Tesla
# Test Using Mox

To mock HTTP requests in your tests using Mox with the Tesla HTTP client,
follow these steps:
Expand Down Expand Up @@ -43,16 +43,19 @@ defmodule MyApp.FeatureTest do
setup context, do: Mox.verify_on_exit!(context)

test "example test" do
#--------- Given - Stubs and Preconditions
# Expect a single HTTP request to be made and return a JSON response
Tesla.Test.expect_tesla_call(
times: 1,
returns: Tesla.Test.json(%Tesla.Env{status: 200}, %{id: 1})
)

#--------- When - Run the code under test
# Make the HTTP request using Tesla
# Mimic a use case where we create a user
assert :ok = create_user!(%{username: "johndoe"})

#--------- Then - Assert postconditions
# Verify that the HTTP request was made and matches the expected parameters
Tesla.Test.assert_received_tesla_call(env, [])
Tesla.Test.assert_tesla_env(env, %Tesla.Env{
Expand All @@ -76,11 +79,6 @@ defmodule MyApp.FeatureTest do
end
```

Important Notes:

- Verify Expectations: Include `setup :verify_on_exit!` to automatically verify
that all `Mox` expectations are met after each test.

## 4. Run Your Tests

When you run your tests with `mix test`, all HTTP requests made by Tesla will
Expand Down

0 comments on commit ca26020

Please sign in to comment.