From b61bac196607edea7db07ac6307b41840a892250 Mon Sep 17 00:00:00 2001 From: Joel Kemp Date: Thu, 17 Sep 2020 09:45:08 -0400 Subject: [PATCH 1/3] Docs around mocking based on parts of the input --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index c250eee..7f263b1 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,33 @@ defmodule MyTest do end ```` +If you want to return a different value based on part of the input (i.e., if the url contains a particular string), you can do as follows: + +```` elixir +defmodule MyTest do + use ExUnit.Case, async: false + + import Mock + + test "mock functions with multiple returns" do + with_mock(Map, [ + get: fn + (%{}, url) -> + cond do + String.contains?(url, ".com") -> "Hello from example.com" + String.contains?(url, ".org") -> "example.org says hi" + end + end + ]) do + assert Map.get(%{}, "http://example.com") == "Hello from example.com" + assert Map.get(%{}, "http://example.org") == "example.org says hi" + end + end +end +```` + +* This is helpful if, for example, you want to mock a method that calls out to different backends based on the input. You can then mock out those various calls based on parts of the input to that function. + ## Mocking functions with different arities You can mock functions in the same module with different arity: From 25bc55a72e75d80f29ebcaa04f8786109b6481b5 Mon Sep 17 00:00:00 2001 From: Joel Kemp Date: Fri, 18 Sep 2020 11:55:38 -0400 Subject: [PATCH 2/3] Don't use a separate section --- README.md | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7f263b1..a454327 100644 --- a/README.md +++ b/README.md @@ -195,42 +195,25 @@ defmodule MyTest do get: fn (%{}, "http://example.com") -> "Hello from example.com" (%{}, "http://example.org") -> "example.org says hi" + (%{}, url) -> conditionally_mocked(url) end ]) do assert Map.get(%{}, "http://example.com") == "Hello from example.com" assert Map.get(%{}, "http://example.org") == "example.org says hi" + assert Map.get(%{}, "http://example.xyz") == "Hello from example.xyz" + assert Map.get(%{}, "http://example.tech") == "example.tech says hi" end end -end -```` - -If you want to return a different value based on part of the input (i.e., if the url contains a particular string), you can do as follows: - -```` elixir -defmodule MyTest do - use ExUnit.Case, async: false - - import Mock - - test "mock functions with multiple returns" do - with_mock(Map, [ - get: fn - (%{}, url) -> - cond do - String.contains?(url, ".com") -> "Hello from example.com" - String.contains?(url, ".org") -> "example.org says hi" - end - end - ]) do - assert Map.get(%{}, "http://example.com") == "Hello from example.com" - assert Map.get(%{}, "http://example.org") == "example.org says hi" + + def conditionally_mocked(url) do + cond do + String.contains?(url, ".xyz") -> "Hello from example.xyz" + String.contains?(url, ".tech") -> "example.tech says hi" end end end ```` -* This is helpful if, for example, you want to mock a method that calls out to different backends based on the input. You can then mock out those various calls based on parts of the input to that function. - ## Mocking functions with different arities You can mock functions in the same module with different arity: From 5809dba3864a049d14b987924fe4fdf78d1add6b Mon Sep 17 00:00:00 2001 From: Daniel Olshansky Date: Fri, 18 Sep 2020 09:00:32 -0700 Subject: [PATCH 3/3] Fix whitespace --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a454327..f930379 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ defmodule MyTest do get: fn (%{}, "http://example.com") -> "Hello from example.com" (%{}, "http://example.org") -> "example.org says hi" - (%{}, url) -> conditionally_mocked(url) + (%{}, url) -> conditionally_mocked(url) end ]) do assert Map.get(%{}, "http://example.com") == "Hello from example.com"