2016-04-23 84 views
1

我是Elixir和Phoenix的新手,我正在嘗試創建一個Web服務來讚揚我的網站。首先,我只想通過從json文件導入一些數據來測試我的新數據結構。我想我會通過測試來做到這一點。我已經閱讀了基本指南(包括測試部分),但是在測試api調用時我還找不到任何東西。用Elixir/Phoenix測試api呼叫

從下面的代碼中,我得到以下錯誤,當我運行mix test

** (ArgumentError) flash not fetched, call fetch_flash/2 

這失敗上,使呼叫並將這個連接線。我假設我使用錯誤的電話/丟失了一些東西?有沒有我錯過的文檔,或有人可以指示我一個很好的例子?

下面是從我router.ex片段:

scope "/", ContactsApp do 
    pipe_through :browser # Use the default browser stack 

    get "/", PageController, :index 
    resources "/contacts", ContactsController 
    end 

    # Other scopes may use custom stacks. 
    scope "/api", ContactsApp do 
    pipe_through :api 

    get "/import", ContactsController, :import 
    end 

暫時,都是我所做的就是複製ContactsController.Create方法並把它稱爲ContactsController.Import。我也複製了「創建資源和重定向時數據是有效的」測試和使用:import代替:create

下面是完整的堆棧跟蹤:

** (ArgumentError) flash not fetched, call fetch_flash/2 
stacktrace: 
    (phoenix) lib/phoenix/controller.ex:997: Phoenix.Controller.get_flash/1 
    (phoenix) lib/phoenix/controller.ex:982: Phoenix.Controller.put_flash/3 
    (contacts_app) web/controllers/contacts_controller.ex:74: ContactsApp.LogController.stuff/2 
    (contacts_app) web/controllers/contacts_controller.ex:1: ContactsApp.LogController.action/2 
    (contacts_app) web/controllers/contacts_controller.ex:1: ContactsApp.LogController.phoenix_controller_pipeline/2 
    (contacts_app) lib/phoenix/router.ex:261: ContactsApp.Router.dispatch/2 
    (contacts_app) web/router.ex:1: ContactsApp.Router.do_call/2 
    (contacts_app) lib/contacts_app/endpoint.ex:1: ContactsApp.Endpoint.phoenix_pipeline/1 
    (contacts_app) lib/phoenix/endpoint/render_errors.ex:34: ContactsApp.Endpoint.call/2 
    (phoenix) lib/phoenix/test/conn_test.ex:194: Phoenix.ConnTest.dispatch/5 
    test/controllers/contacts_controller_test.exs:69 
+0

你在代碼或測試的某個地方調用'get_flash'嗎? – Dogbert

+0

顯然'conn = get conn,contacts_path(conn,:import),log:@ valid_attrs'確實 – Mitkins

+1

您試過了'mix phoenix.gen.json'命令嗎?它會爲你生成測試。所以你可以看到它是如何完成的。 – sobolevn

回答

1

感謝@Dogbert和@sobolevn我能圖我做錯了什麼。當通過mix phoenix.gen.html生成的代碼,則該控制器可以具有類似於下面的:

def create(conn, %{"contact" => contact_params}) do 
    changeset = Contact.changeset(%Contact{}, contact_params) 

    case Repo.insert(changeset) do 
    {:ok, _contact} -> 
     conn 
     |> put_flash(:info, "Contact created successfully.") 
     |> redirect(to: contact_path(conn, :index)) 
    {:error, changeset} -> 
     render(conn, "new.html", changeset: changeset) 
    end 
end 

當通過mix phoenix.gen.json生成的代碼,則該控制器包含略有不同的代碼:

def create(conn, %{"fred" => fred_params}) do 
    changeset = Fred.changeset(%Fred{}, fred_params) 

    case Repo.insert(changeset) do 
    {:ok, fred} -> 
     conn 
     |> put_status(:created) 
     |> put_resp_header("location", fred_path(conn, :show, fred)) 
     |> render("show.json", fred: fred) 
    {:error, changeset} -> 
     conn 
     |> put_status(:unprocessable_entity) 
     |> render(ContactsApp.ChangesetView, "error.json", changeset: changeset) 
    end 
end 

在代碼我複製並粘貼,如@Dogbert所示(這是爲了與:browser管道協同工作),致電put_flash。使用使用mix phoenix.gen.json生成的代碼修復了問題。