tamagohan2
tamagohan2

Reputation: 445

How to use meck in different ExUnit test file

I would like to use meck in different ExUnit test file.

For example,

[x_test.exs]

def setup do
  :meck.new(Hoge, [:passthrough])
 on_exit(fn -> :meck.unload end)
 :ok
end

def teardown do
  :meck.unload
end

test "foo" do
  :meck.expect(Hoge, :foo, fn -> 1 end)
  assert Hoge.foo == 1
end

[y_test.exs]

def setup do
  :meck.new(Hoge, [:passthrough])
 on_exit(fn -> :meck.unload end)
 :ok
end

def teardown do
  :meck.unload
end

test "foo" do
  :meck.expect(Hoge, :foo, fn -> 2 end)
  assert Hoge.foo == 2
end

Sometimes, x_test.exs is fail, but sometimes, x_test.exs is success... (y_test.exs is same)

Can I use mock to same function in another test file?

Upvotes: 1

Views: 456

Answers (1)

Lol4t0
Lol4t0

Reputation: 12547

meck currently compiles and loads code you specified with your expectations. As only one current version of the code could be loaded inside the beam you should execute all tests that race for the same mocked function sequentially.

As ExUnit documentation states that test cases are executed in parallel, you probably have to merge all the tests that should be executed serially in the single test case (i.e. single test module).

Alternatively, you could set number of test cases that could be executed in parallel to 1. However it could slow down your test run

ExUnit supports the following options:

  • :max_cases - maximum number of cases to run in parallel; defaults to :erlang.system_info(:schedulers_online)

Upvotes: 2

Related Questions