如何控制测试Elixir / Phoenix中的独特约束

时间:2016-06-22 16:05:58

标签: elixir phoenix-framework ecto

跟进Making a field unique in ectoTesting Validations in Elixir and Ecto,我无法为唯一约束设置控制器测试:

test "POST /imports will not store duplicate address" do
  # Create the existing (conflicting) record:
  location = "melee island"
  MyApp.Repo.insert! %MyApp.Location{address: location}

  post("/imports", %{"location" => location})

  # Other assertions omitted

  # Check if the location was indeed NOT inserted
  query = from l in MyApp.Location,
    where: [address: ^location],
    select: l.address

  assert MyApp.Repo.all(query) == [location]
end

最后一个断言失败,出现以下错误:

  

(Postgrex.Error)错误(in_failed_sql_transaction):当前事务被中止,命令被忽略直到事务块结束

我理解为什么 - 所有内容都包含在测试事务中 - 但是我不能在不丢失最初插入的记录的情况下回滚。有没有办法测试这种行为?

0 个答案:

没有答案
相关问题