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

Mix test --no-start #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Example.MixProject do
elixir: "~> 1.9",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps()
]
end
Expand All @@ -32,4 +33,10 @@ defmodule Example.MixProject do
{:mox, "~> 0.5.1", only: :test}
]
end

def aliases do
[
test: "test --no-start"
]
end
end
12 changes: 12 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# In order to use Mox.Server we need to ensure all applications for
# dependencies are loaded, and to call `Appplication.spec/2` requires that
# the application has been loaded
import Application

load(:example)
Enum.each(spec(:example, :applications), &ensure_all_started/1)

# Define our mocks
Mox.defmock(Example.MockService, for: Example.ServiceBehaviour)

# Start the test framework
ExUnit.start()
23 changes: 6 additions & 17 deletions test/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,18 @@ defmodule Example.WorkerTest do
import Mox
alias Example.Worker

describe "default service" do
test "returns default service foo" do
assert Worker.get_foo() =~ ~s(default says foo)
end
end
setup :verify_on_exit!

describe "mocked service" do
setup do
# Normally you would add this to `test_helper.ex`, or `support/mocks.ex
Mox.defmock(Example.MockService, for: Example.ServiceBehaviour)

Example.MockService
|> expect(:foo, fn -> "setup all says foo" end)

:ok
end

setup :verify_on_exit!
# We have to set Mox to global because we would have an impossible chicken
# and egg situation wtih Mox.allow/2 and Worker.start_link/0
setup :set_mox_global

test "returns mocked service foo" do
Example.MockService
|> expect(:foo, fn -> "mock says foo" end)
|> allow(self(), Process.whereis(Worker))

Worker.start_link()

assert Worker.get_foo() =~ ~s(mock says foo)
end
Expand Down