跟进Making a field unique in ecto和Testing 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):当前事务被中止,命令被忽略直到事务块结束
我理解为什么 - 所有内容都包含在测试事务中 - 但是我不能在不丢失最初插入的记录的情况下回滚。有没有办法测试这种行为?