Modular v0.3.2 Modular.AreaAccess View Source

Allows to declare external deps and optionally resolve them to mocks.

Usage

Define area behaviours and implementations:

defmodule MyApp.First do
  @callback some() :: :ok
end

defmodule MyApp.Second do
  @callback other() :: :ok
end

defmodule MyApp.First.Impl do
  @behaviour MyApp.First

  def some do
    :ok
  end
end

defmodule MyApp.Second.Impl do
  @behaviour MyApp.Second

  def other do
    :ok
  end
end

Declare dependencies and call other areas:

defmodule MyApp.First.Impl do
  @behaviour MyApp.First

  use Modular.AreaAccess, [
    MyApp.Second
  ]

  def some do
    impl(MyApp.Second).other()
  end
end

Mocking

Enable mocking and configure areas that are viable for it in config/test.exs:

config :modular,
  area_mocking_enabled: true,
  areas: [
    MyApp.First,
    MyApp.Second
  ]

Setup mocks for Mox intest/suport/mocks.ex (follow Mox docs on including the test/support directory in compile paths):

Modular.AreaAccess.define_mocks()

Then allow usage of any areas from test cases and stub them back to real implementations:

defmodule MyCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      use Modular.AreaAccess, :all
    end
  end

  setup do
    Modular.AreaAccess.install_stubs()

    :ok
  end
end

Write tests as normal and mock with custom expectations when needed:

defmodule MyTest do
  use MyCase

  test "normal case" do
    assert :ok = impl(MyApp.First).some()
  end

  test "mocked case" do
    Mox.expect(impl(MyApp.Second), :other, fn -> :mocked end)

    assert :mocked = impl(MyApp.First).some()
  end
end

Link to this section Summary

Link to this section Functions