Tôi đi theo this tutorial và thử nghiệm đơn giản của tôi luôn thất bại với lỗi này"Không thể bắt đầu giao dịch thử nghiệm bởi vì chúng ta đã bên trong một"
1) test /index returns a list of contacts (WorldNote.ChatsControllerTest)
test/controllers/chats_controller_test.exs:16
** (RuntimeError) cannot begin test transaction because we are already inside one
stacktrace:
(ecto) lib/ecto/adapters/sql.ex:321: anonymous fn/6 in Ecto.Adapters.SQL.start_test_transaction/3
(ecto) lib/ecto/adapters/sql.ex:615: Ecto.Adapters.SQL.pool_transaction/4
(ecto) lib/ecto/adapters/sql.ex:314: Ecto.Adapters.SQL.start_test_transaction/3
test/controllers/chats_controller_test.exs:9: WorldNote.ChatsControllerTest.__ex_unit_setup_0/1
test/controllers/chats_controller_test.exs:1: WorldNote.ChatsControllerTest.__ex_unit__/2
Mã này là khá đơn giản
defmodule WorldNote.ChatsControllerTest do
use ExUnit.Case, async: false
use Plug.Test
alias WorldNote.Chats
alias WorldNote.Repo
alias Ecto.Adapters.SQL
setup do
SQL.begin_test_transaction(Repo)
on_exit fn ->
SQL.rollback_test_transaction(Repo)
end
end
test "/index returns a list of contacts" do
contacts_as_json =
%Chats{fbid: 1234567890, latitude: 0.0, longitude: 0.0, content: "yo"}
|> Repo.insert
|> List.wrap
|> Poison.encode!
response = conn(:get, "/api/contacts") |> send_request
assert response.status == 200
assert response.resp_body == contacts_as_json
end
defp send_request(conn) do
conn
|> put_private(:plug_skip_csrf_protection, true)
|> WorldNote.Endpoint.call([])
end
end
Tôi googled khắp nơi lỗi cannot begin test transaction because we are already inside one
. Nhưng không thể tìm thấy bất kỳ sửa chữa nào.
PS. Im sử dụng Postgresql
Khắc phục sự cố của bạn cho vấn đề này là gì? Tôi vừa cập nhật từ Phoenix_Ecto 0,3 đến 0,4 và bây giờ tôi nhận được vấn đề này trên mọi thử nghiệm :(- ngay cả trong thử nghiệm mô hình của tôi mà không sử dụng bộ điều khiển – TheStoneFox
Giống như @JoseValim nói. ExUnit tự động thực hiện việc này kể từ khi cập nhật –