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

Proposal: implementation of struct: option for defmock #91

Closed
wants to merge 1 commit into from

Conversation

ityonemo
Copy link
Contributor

In some cases I have found myself wanting to be able to clone a struct from an implementation module to build a mock. This PR creates a :struct option for defmock, which drags the source module's struct into the mock implementation. Before cloning, the struct-bearing module is checked to see if it matches the behaviours in the mock module. Hopefully this fits in the spirit of mocks being nouns, not verbs, and doesn't interfere too much with the nominal operation of mox!

@josevalim
Copy link
Member

Hi @ityonemo. Apologies but I am not convinced this should be the case.

If there is a struct in your mock, then the struct is part of the contract, an it should be properly defined as part of your expectations. Ideally, since this is a mock, I don't think you should be using structs at all, and instead define the proper functions that work on these values.

@ityonemo
Copy link
Contributor Author

ityonemo commented Apr 17, 2020

no problem! I just had a protocol functions that detected %module{} and wanted to stub them... I'm probably just using protocols wrong.

eg:

defprotocol Proto do
  def func(t)
end

defimpl Proto, for: MyModule do
  def func(thing = %module{}) do
   # lets me selectively stub in implementation testing
    module.do_something(thing)
    module.do_something_else(thing)
  end
end

defmock Mock, for: MyModule.Api, struct: MyModule
defimpl Proto, for: Mock do
  defdelegate func(thing), to: MyModule
end

@ityonemo ityonemo closed this Apr 17, 2020
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.

None yet

2 participants