2

我想测试一个函数如何改变数据库中的某些内容。我正在努力使用与以下ActiveSupport::TestCase测试用例等效的 ExUnit:

test "creates a database record" do
  post = Post.create title: "See the difference"
  assert_difference "Post.published.count" do
    post.publish!
  end
end

RSpec 版本更加优雅,并且由于它使用了 lambda,我认为它可以移植到 Elixir/ExUnit。

it "create a database record" do
  post = Post.create title: "See the difference"
  expect { post.publish! }.to change { Post.count }.by 1
end

有没有比这更优雅(阅读:功能)的方式来做到这一点:

test "creates a database record", %{conn: conn} do
  records_before = count_records
  post(conn, "/articles")
  records_after  = count_records

  assert records_before == (records_after - 1)
end

defp count_records do
  MyApp.Repo.one((from a in MyApp.Article, select: count("*"))
end
4

1 回答 1

2

您可以使用宏从 Ruby 中获取接近 TestUnit 和 RSpec 示例的内容:

defmacro assert_difference(expr, do: block) do
  quote do
    before = unquote(expr)
    unquote(block)
    after_ = unquote(expr)
    assert before != after_
  end
end

defmacro assert_difference(expr, [with: with], do: block) do
  quote do
    before = unquote(expr)
    unquote(block)
    after_ = unquote(expr)
    assert unquote(with).(before) == after_
  end
end

test "the truth" do
  {:ok, agent} = Agent.start_link(fn -> 0 end)

  assert_difference Agent.get(agent, &(&1)) do
    Agent.update(agent, &(&1 + 1))
  end

  {:ok, agent} = Agent.start_link(fn -> 0 end)

  assert_difference Agent.get(agent, &(&1)), with: &(&1 + 2) do
    Agent.update(agent, &(&1 + 2))
  end
end

但是我不会使用它,除非它会被大量使用,否则这只会使除作者之外的所有人(可能)更难理解代码。如果您确实使用它,您可能希望将其移动到不同的模块并将其导入您的测试模块中。

于 2016-09-28T10:41:49.227 回答