-
-
Save bigfive/17136e5f2a7453121f3fb8695734ff41 to your computer and use it in GitHub Desktop.
Affiliate Service Dependency System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A module that uses dependencies | |
defmodule EventStore.MyModule do | |
defp stream_filterer, do: EventStore.Dependencies.get(:stream_filterer) | |
def something do | |
stream_filterer.do_the_things | |
end | |
end | |
# A test for the module that uses dependencies | |
defmodule EventsStore.MyModuleTest do | |
defmodule FakeStreamFilterer do | |
def do_the_things do | |
# ... | |
end | |
end | |
test "it does the right stuff" do | |
EventStore.Mocks.update(:stream_filterer, FakeStreamFilterer) | |
#... | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config | |
config :event_store, :dependencies, | |
stream_filterer: EventStore.StreamFilterer | |
# dependency module | |
# Just wraps `Application.fetch_env` | |
defmodule EventStore.Dependencies do | |
def get(key), do: config[key] | |
defp config do | |
Application.fetch_env!(:event_store, :dependencies) | |
end | |
end | |
# A example dependency | |
defmodule EventStore.StreamFilterer do | |
def do_the_things do | |
# ... | |
end | |
end | |
# The test Helper that updates dependencies | |
defmodule EventsStore.Mocks do | |
def update(key, module) do | |
# Updates the appropriate config | |
# Ensures the config resets after the test | |
# Ensures that the module you are swapping in has the same functions as the default module | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment