stephanos / rewire

Dependency injection for Elixir. Zero code changes required.

Home Page:https://hex.pm/packages/rewire

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to rewire deps with the same unscoped module name as module under test?

parkerjm opened this issue · comments

Given two modules:

  • App.Connector.Hubspot (used by app to interact with external resource)
  • App.Client.Hubspot (used by connector to make external calls)

It is not possible to (easily?) inject a mock for the client in the connector tests, since:

  • elixir does not support the syntax: rewire App.Connector.Hubspot, App.Client.Hubspot: HubspotMock
  • aliasing App.Client.Hubspot is ambiguous since the connector has the same module name

It seems plausible it could work with some alias tweaking, but this could be more easily solved by accepting a list of tuples instead of a keyword list, perhaps? Hopefully I can look more into this later.

I got it to work now, just had to namespace the mock like App.Client.HubspotMock then alias it. I guess the namespace of the mock has to match the namespace of the dependency. Maybe this just needs some documentation? I can make a PR if you agree.

Please disregard everything above. I just had some misunderstandings about what the library does. I see now.

:) I'd still be interested to hear whether you think I can make something clearer in the docs/README!

my misconception, which makes little sense in retrospect, was that I thought this lib removed the need to define behaviors for every module I wanted to mock when using Mox. now I understand that I still need behaviors and mock module definitions, and that this lib removes the need to have extra config everywhere to inject modules. after discussing the issue, I realized that having these behaviors is a good thing anyways, and that I could simply nest the behavior definition inside the modules to prevent having a ton of behavior files laying around.

anyways, the ability to remove all of the dep injection config and lookup function calls is still a huge win and I am a happy customer. :) thanks for making this.

awesome, glad to hear that! Yeah, I saw it from the unix philosophy of "make each program do one thing well" :)

@parkerjm could you expand on what you saw back then? I don't understand how it is supposed to work :)

Nvm my problem wasn't related, I had to include this in my test helper:

Application.ensure_all_started(:rewire)

Else I get:

(UndefinedFunctionError) function :cover.compile_beams/1 is undefined or private.