2017-04-16 126 views
0

我有用於登錄用戶的端點。我編寫了集成測試和控制器測試,但它們看起來非常相似,除了我在控制器測試中測試了更多的邊緣案例(例如,用戶嘗試使用不正確的電子郵件登錄)。測試的內容似乎並沒有什麼不同,所以如果有人能夠闡明他們應該如何改變,那將是非常好的。Phoenix:API集成測試vs控制器測試

集成測試:

defmodule SigningInUserTest do 
    use ParrotApi.ConnCase 
    alias ParrotApi.Router 

    @opts Router.init([]) 
    describe "POST api/v1/sessions" do 
    test "success" do 
     email = "[email protected]" 
     password = "ilovemodals" 
     user = insert(:user, 
        email: email, 
        password_hash: Comeonin.Bcrypt.hashpwsalt(password)) 
     user_params = %{ 
     user: %{ 
      email: user.email, 
      password: password, 
     } 
     } 

     conn = build_conn(:post, "/api/v1/sessions", user_params) 
     response = Router.call(conn, @opts) 
     assert response.status == 201 
    end 

    test "failure" do 
     email = "[email protected]" 
     password = "ilovemodalszzz" 
     user = insert(:user, 
        email: email, 
        password_hash: Comeonin.Bcrypt.hashpwsalt(password)) 
     user_params = %{ 
     user: %{ 
      email: user.email, 
      password: "bad_password", 
     } 
     } 

     conn = build_conn(:post, "/api/v1/sessions", user_params) 
     response = Router.call(conn, @opts) 
     assert response.status == 401 
    end 
    end 

控制器測試:

defmodule ParrotApi.SessionControllerTest do 
    use ParrotApi.ConnCase 

    setup %{conn: conn} do 
    {:ok, conn: put_req_header(conn, "accept", "application/json")} 
    end 

    describe "#create" do 
    test "returns the user when the email and password match", %{conn: conn} do 
     email = "[email protected]" 
     password = "ilovemodals" 
     user = insert(:user, 
        email: email, 
        password_hash: Comeonin.Bcrypt.hashpwsalt(password)) 
     rsvp = insert(:rsvp, user: user) 
     user_params = %{ 
     user: %{ 
      email: email, 
      password: password, 
     } 
     } 
     conn = conn 
      |> post(session_path(conn, :create), user_params) 
     assert json_response(conn, 201)["data"] == %{ 
     "id" => user.id, 
     "email" => user.email, 
     "name" => user.name, 
     "interests" => user.interests, 
     "location" => user.location, 
     "image_url" => user.image_url, 
     "is_admin" => user.is_admin, 
     "rsvps" => [rsvp.meetup_id], 
     } 
    end 


    test "returns an error when the email and password don't match", %{conn: conn} do 
     email = "[email protected]" 
     password = "ilovemodals" 
     insert(:user, 
      email: email, 
      password_hash: Comeonin.Bcrypt.hashpwsalt(password)) 
     user_params = %{ 
     user: %{ 
      email: email, 
      password: "bad_password", 
     } 
     } 
     conn = conn 
      |> post(session_path(conn, :create), user_params) 
     assert json_response(conn, 401) == %{ 
     "message" => "The email and password you entered did not match our records. Please try again." 
     } 
    end 

    test "returns an error when the user doesn't exist", %{conn: conn} do 
     user_params = %{ 
     user: %{ 
      email: "[email protected]", 
      password: "bad_password", 
     } 
     } 
     conn = conn 
      |> post(session_path(conn, :create), user_params) 
     assert json_response(conn, 401) == %{ 
     "message" => "The email and password you entered did not match our records. Please try again." 
     } 
    end 
    end 
end 

回答

2

你的集成測試似乎並沒有被測試任何控制器的測試已經覆蓋。但是,我相信你已經知道這一點。

您的控制器測試未涵蓋的是通過Web服務器的路徑。我會建議啓用服務器進行集成測試,並使用像HttpPoison這樣的http客戶端來完成實際的請求。

我還建議您將這些測試標記爲集成測試。如果你發現他們正在放慢你的測試,那麼默認禁用它們。我們使用hound進行ExAdmin集成測試,我們使用真正的瀏覽器填充和點擊。這些測試默認是禁用的。要做到這一點:

# my_app/config/test.exs 
use Mix.Config 
config :my_app, TestMyApp.Endpoint, 
    http: [port: 4001], 
    server: true 

# my_app/test/test_helpers.exs 
ExUnit.configure(exclude: [pending: true, integration: true]) 
ExUnit.start() 

然後用運行它們:

# Run all tests 
mix test --include integration 
# or run only integration 
mix test --only integration