Skip to content

Commit

Permalink
[amarandon] Document that it's not possible to mock marcos (#126)
Browse files Browse the repository at this point in the history
Fix #125
  • Loading branch information
amarandon committed Nov 11, 2020
1 parent 987a67e commit e00a887
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ defmodule MyTest do
end
````

## NOT SUPPORTED - Mocking internal function calls
## NOT SUPPORTED

### Mocking internal function calls

A common issue a lot of developers run into is Mock's lack of support for mocking
internal functions. Mock will behave as follows:
Expand Down Expand Up @@ -522,6 +524,36 @@ Or, like so:
end
````

### Mocking macros

Currently mocking macros is not supported. For example this will not work because `Logger.error/1` is a macro:

```elixir
with_mock Logger, [error: fn(_) -> 42 end] do
assert Logger.error("msg") == 42
end
```

This code will give you this error: `Erlang error: {:undefined_function, {Logger, :error, 1}}`

As a workaround, you may define a wrapper function for the macro you need to invoke:

```elixir
defmodule MyModule do
def log_error(arg) do
Logger.error(arg)
end
end
```

Then in your test you can mock that wrapper function:

```elixir
with_mock MyModule, [log_error: fn(_) -> 42 end] do
assert MyModule.log_error("msg") == 42
end
```

## Tips
The use of mocking can be somewhat controversial. I personally think that it
works well for certain types of tests. Certainly, you should not overuse it. It
Expand Down

0 comments on commit e00a887

Please sign in to comment.