Skip to content
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

Merged
merged 2 commits into from
Jul 9, 2020

Conversation

flurin
Copy link
Contributor

@flurin flurin commented Jul 8, 2020

Checks if the mock actually exports the function before it defines the stub. Fixes #95

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
Copy link
Member

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 .

Copy link
Contributor Author

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?

Copy link
Member

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
Comment on lines 525 to 527
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
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

@josevalim josevalim merged commit 4dd4c9b into dashbitco:master Jul 9, 2020
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mox.stub_with/2 does not work with skip_optional_callbacks
2 participants