with_mock and Behaviours
joebew42 opened this issue · comments
Hello folks,
I started to use Elixir and since I like to practice TDD/test-first-programming when I am at writing code I find useful to use this library to support me during the phase of roles discovery
, which means I know I need to use a collaborator but I haven't enough information to implement it, I would like that the discovery of the role is guided by tests.
In Java, an example of an implementation of the concept of roles are interfaces. In Elixir we have Behaviours.
I tried to create a mock of a behaviour using this library but something is wrong, or I am missing something. Maybe it is not supported yet. Here is an example of what I am trying to do:
A role: collaborator.ex
defmodule Collaborator do
@callback save(String.t) :: {:ok, String.t}
end
A test: a Client that need to use the Collaborator
test "we call a collaborator" do
with_mock Collaborator, [save: fn(_name) -> "this is a test" end] do
response = Client.do_something()
assert "this is a test" == response
end
end
A client
defmodule Client do
@collaborator Collaborator
def do_something() do
@collaborator.save("something")
end
end
At the moment if I try to run the test I get:
** (ErlangError) Erlang error: {:undefined_function, {Collaborator, :save, 1}}
code: with_mock Collaborator, [save: fn(_name) -> "this is a test" end] do
Maybe I am missing something, or I get this because mock don't support Behaviours
at time. Do you think that the support of Behaviours
in mock can be a good feature to add?
Thanks,
joe.
If you use behaviors you should consider using Mox, which has the advantage of letting your tests run in parallel.
Thank you @JoanZapata , it seems that Mox is exactly what I am looking for 👍 I will try it as soon as possibile, thank you!