I have similar tests in controllers and the setup_all
inserts are available in the test blocks. Here I'm trying to allow the same thing in my models and I can't seem to make it work.
I have a test as follows:
defmodule Faq.QuestionTest do
use Faq.ModelCase
alias Faq.Question
setup_all do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
Question.changeset(%Question{}, %{question: "Unanswered", answer: nil}) |> Repo.insert!
Question.changeset(%Question{}, %{question: "Answered", answer: "My published answer", published_at: Ecto.DateTime.utc(:usec)}) |> Repo.insert!
published_count = Question |> Question.published |> Repo.all |> Enum.count
assert 1 == published_count
IO.puts "SETUP_ALL"
:ok
end
describe "scopes" do
test "answered", meta do
published_count = Question |> Question.published |> Repo.all |> Enum.count
assert 1 == published_count
end
end
end
When I run it I have the following error:
$ mix test test/models/question_test.exs
warning: variable meta is unused
test/models/question_test.exs:21
SETUP_ALL
1) test scopes answered (Faq.QuestionTest)
test/models/question_test.exs:21
Assertion with == failed
code: 1 == published_count
lhs: 1
rhs: 0
stacktrace:
test/models/question_test.exs:23: (test)
Finished in 0.09 seconds
1 test, 1 failure
Now in the setup_all
block I make the same validation as I do in my test
block. Why would it fail in the test but pass in the setup_all
?
So it turns out that
ModelCase
was running the first line ofsetup_all
again on each run thus invalidating my connection. This line right here::ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)