0
我在控制器中有類似的測試,在測試塊中有setup_all
插入。在這裏,我試圖在我的模型中允許同樣的事情,而且我似乎無法使其工作。爲什麼在setup_all塊的數據庫中插入的對象不會出現在測試塊中?
我有一個測試如下:
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
當我運行它,我有以下錯誤:
$ 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
現在在setup_all
塊我做出同樣的驗證,因爲我在做我的test
區塊。爲什麼它會在測試中失敗,但通過setup_all
?
'setup'而不是'setup_all'對你來說是否正常工作? – Dogbert