dashbitco / mox

Mocks and explicit contracts in Elixir
1.35k stars 77 forks source link

Feature Request: Ability to generate a mock that doesn't implement optional callbacks #61

Closed axelson closed 5 years ago

axelson commented 5 years ago

For some of my tests it would be useful to generate a Mock that does not include optional callbacks (because the specific module that it is mocking also does not define them).

Possible workarounds:

Would a PR be accepted that adds this functionality?

josevalim commented 5 years ago

Definitely, a PR is welcome. I think though the only option is the second one. Do you have an idea of how the API would look like?

whatyouhide commented 5 years ago

Do you always want to not generate any of the optional callbacks? As in, would something like this work?

Mox.defmock(MyMock, for: MyBehaviour, optional_callbacks: false)
axelson commented 5 years ago

I was thinking something similar. Along the lines of:

Mox.defmock(
  MyMock,
  for: MyBehaviour,
  optional_callbacks: [on_success: 2, on_failure: 1]
)

So we can piggy back on the same syntax as specifying the optional callbacks in the first place. And if you don't want any of the optional callbacks defined then just pass an empty list. If you want the default behavior of all the optional callbacks then you would pass an atom like :all.

josevalim commented 5 years ago

@axelson in this we would need the negated version then:

Mox.defmock(
  MyMock,
  for: MyBehaviour,
  skip_optional_callbacks: [on_success: 2, on_failure: 1]
)
axelson commented 5 years ago

@josevalim I don't feel too strongly either way, and have begun implementation on :skip_optional_callbacks. But since I generally have a preference for whitelist approaches (as opposed to blacklists) I was wondering if you could share the reasoning behind your preference for the :skip_optional_callbacks approach.

josevalim commented 5 years ago

@axelson because the default is to include everything, it would be weird IMO if to skip a single one, then I need to list all of them. Does that make any sense?

axelson commented 5 years ago

Ah, I see. That does make sense 👍

On Tue, Apr 23, 2019, 8:45 AM José Valim notifications@github.com wrote:

@axelson https://github.com/axelson because the default is to include everything, it would be weird IMO if to skip a single one, then I need to list all of them. Does that make any sense?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/plataformatec/mox/issues/61#issuecomment-485927193, or mute the thread https://github.com/notifications/unsubscribe-auth/AAACN5O2FMSBR3G54LBXMCTPR5KMVANCNFSM4HHS7EEA .