I am trying to run unit tests in a phoenix project. The tests I am writing mock functions that call Ecto.Adapters.SQL.Query() so I do not need a database connection for the tests to succeed.
When I run the tests with $ mix test/unit/my_test.exs I get the following error. I have no Idea how I can disable the repository connection attempt, any ideas?
defmodule Test.Unit.MyTest do
import Mock
use ExUnit.Case
@moduletag :unit_test
describe "one equals two" do
test "returns true" do
assert 1 == 2
end
end
end
This is likely coming from the Repo configuration in your test environment config. Usually residing in config/test.exs. The Repo starts with your app so you’ll have to remove it to get rid of the error (or start it manually on demand).
The good question here is: do you not need a DB at all in all tests, or you want part of the tests to never hit it?
I do need a database for my integration and system tests of course, but I want to be able to run the unit tests separately from them without getting a failure because of a missing database connection.
Do I need to set up a separate test config for unit tests then?
Edit: Removing the connection config doesn’t help, just makes the compilation crash because of missing database connection information
I am now running unit tests like this: $ mix test --only unit_test --no-start where :unit_test is the module tag on all test modules that are unit tests only
For this to work, I had to comment out the Ecto.Adapters.SQL.Sandbox.mode(App.Repo, :manual) line in the test_helper.exs file
Also if you have some aliases in mix.exs file for test command like ecto.create, you need to remove them. I was trying to understand Ecto calls when running the unit tests.