0

我在控制器中进行了类似的测试,并且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

4

1 回答 1

0

所以事实证明,每次运行时都会再次ModelCase运行第一行,setup_all从而使我的连接无效。这一行就在这里::ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)

于 2016-12-24T19:22:06.430 回答