Skip to content

mgwidmann/stub_alias

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stub Alias Hex.pm

A simple macro to allow switching alias statements out with different values based upon environment.

Installation

Available in Hex, the package can be installed as:

Add stub_alias to your list of dependencies in mix.exs:

def deps do
  [{:stub_alias, "~> 0.1.0"}]
end

StubAlias is a compile time dependency and can be left out of the applications list.

Usage

In your code you may have some alias statements like the following:

EXAMPLE

defmodule MyModule do
  alias MyModule.Foo

  def stuff() do
    Foo.do_something_with_side_effects
  end
end

Obviously, during testing, it makes it more difficult to test the function stuff/0 since it calls another function which has undesierable side effects (or requires some system state like a running GenServer).

If you add to your config/test.exs configuration like the following:

config :stub_alias,
  "MyModule.Foo": MyModule.Stubs.Foo

Setup your mix.exs to compile in the test/support folder like in Phoenix. Then replace alias MyModule.Foo with stub_alias MyModule.Foo (after import StubAlias of course):

SOLUTION

lib/my_module.ex

defmodule MyModule do
  import StubAlias
  stub_alias MyModule.Foo

  def stuff() do
    Foo.do_something_with_side_effects
  end
end

test/support/foo.ex

defmodule MyModule.Stubs.Foo do
  def do_something_with_side_effects() do
    # Return hard coded data or get data from an agent or whatever you please
    results = %{}
    results
  end
end

In the :test Mix.env, your aliases will be replaced as desired. This then allows you to have a compiled test/support folder which supplies those stubs, making easy explicit replacements of code at test time.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages