我想测试一个函数如何改变数据库中的某些内容。我正在努力使用与以下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