-
Notifications
You must be signed in to change notification settings - Fork 76
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
Make stub_with work with skip_optional_callbacks #96
Conversation
lib/mox.ex
Outdated
@@ -524,7 +524,9 @@ defmodule Mox do | |||
|
|||
for behaviour <- behaviours, | |||
{fun, arity} <- behaviour.behaviour_info(:callbacks) do | |||
stub(mock, fun, :erlang.make_fun(module, fun, arity)) | |||
if function_exported?(mock, fun, arity) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should only skip if they are optional though. Otherwise we can stop raising for functions that are required .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure because this checks if the mock function had been defined on the mock. So when you use skip_optional_callbacks
with a specific set of callbacks it still works correctly.
Just checking if it's optional wouldn't cover that, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I misunderstood. Looks good as is then!
lib/mox.ex
Outdated
for behaviour <- behaviours, | ||
{fun, arity} <- behaviour.behaviour_info(:callbacks) do | ||
stub(mock, fun, :erlang.make_fun(module, fun, arity)) | ||
if function_exported?(mock, fun, arity) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something along these lines:
for behaviour <- behaviours,
optional = behaviour.behaviour_info(:optional_callbacks),
{fun, arity} <- behaviour.behaviour_info(:callbacks) do
if function_exported?(mock, fun, arity) or {fun, arity} not in optional do
We should add a test that stub_with
fails when some non-optional callback is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that you should always implement the full behaviour in the Stub module, but enforcing this would possibly break a lot of current usage. Currently this test succeeds:
defmodule PartialCalcImplementation do
@behaviour Calculator
def add(x, y), do: x + y
end
test "partial" do
in_all_modes(fn ->
stub_with(CalcMock, PartialCalcImplementation)
assert CalcMock.add(1, 2) == 3
assert_raise UndefinedFunctionError, fn ->
assert CalcMock.mult(3, 4)
end
end)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be happy to change that you can only stub full behaviour implementations of course.
💚 💙 💜 💛 ❤️ |
Checks if the mock actually exports the function before it defines the stub. Fixes #95