### Create Thing and Assert Response using follow_form and assert_response Source: https://hexdocs.pm/phoenix_integration/readme This example demonstrates creating a 'thing' as an admin user and then verifying that a regular user can view it. It uses `test_sign_in_user`, `follow_form`, and `assert_response` to manage user states and confirm expected outcomes. ```elixir test "admin can create a thing", %{conn: conn} do # create and sign in admin admin = test_insert_user permissions: @admin_perms admin_conn = test_sign_in_user(conn, admin) # create and sign in regular user user = test_insert_user user_conn = test_sign_in_user(conn, user) # admin create a new thing get( admin_conn, admin_path(conn, :index) ) |> follow_link( "Create Thing" ) |> follow_form( %{ thing: %{ name: "New Thing" }} ) |> assert_response( status: 200, path: admin_path(conn, :index), html: "New Thing" ) # load the thing thing = Repo.get_by(Thing, name: "New Thing") assert thing # the user should be able to view the thing get( user_conn, page_path(conn, :index) ) |> follow_link( thing.name ) |> assert_response( status: 200, path: thing_path(conn, :show, thing), html: "New Thing" ) end ``` -------------------------------- ### Basic Page Flow Test Source: https://hexdocs.pm/phoenix_integration/index This example demonstrates a basic page flow test. It starts by getting the root index page, then uses `follow_link` to navigate through several pages, and finally asserts the response status and path using `assert_response`. ```elixir test "Basic page flow", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) # click/follow through the various about pages |> follow_link( "About Us" ) |> follow_link( "Contact" ) |> follow_link( "Privacy" ) |> follow_link( "Terms of Service" ) |> follow_link( "Home" ) |> assert_response( status: 200, path: page_path(conn, :index) ) end ``` -------------------------------- ### Create New User Test Source: https://hexdocs.pm/phoenix_integration/index This example tests the user creation flow. It navigates to the sign-up page, fills in the user form using `follow_form` with provided user data, and then asserts the response status, path, and verifies the new user's name is present in the HTML. ```elixir test "Create new user", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) # click/follow through the various about pages |> follow_link( "Sign Up" ) |> follow_form( %{ user: %{ name: "New User", email: "user@example.com", password: "test.password", confirm_password: "test.password" }} ) |> assert_response( status: 200, path: page_path(conn, :index), html: "New User" ) end ``` -------------------------------- ### Phoenix Integration Basic Page Flow Test Source: https://hexdocs.pm/phoenix_integration/readme An example integration test demonstrating a basic page flow using `phoenix_integration`. The test navigates through several links, clicks a button, and returns to the index page, asserting the final status and path. It utilizes the pipe operator `|>` to chain actions sequentially. ```elixir defmodule MyApp.AboutIntegrationTest do use MyApp.IntegrationCase, async: true test "Basic page flow", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) # click/follow through the various about pages |> follow_link( "About Us" ) |> follow_link( "Contact" ) |> follow_link( "Privacy" ) |> follow_link( "Terms of Service" ) |> follow_button( "Accept" ) |> follow_link( "Home" ) |> assert_response( status: 200, path: page_path(conn, :index) ) end end ``` -------------------------------- ### Simulate Multiple Users and Permissions Test Source: https://hexdocs.pm/phoenix_integration/index This example demonstrates simulating multiple users and testing permission management. It signs in a user and an admin, checks if the user can access a restricted page (they cannot), grants permissions via the admin interface, and then verifies the user can now access the restricted content. It highlights tracking separate connections (`user_conn`, `admin_conn`) for different user states. ```elixir test "admin grants a user permissions", %{conn: conn, user: user, admin: admin} do # sign in the user and admin user_conn = test_sign_in( conn, user ) admin_conn = test_sign_in( conn, admin ) # user can't see a restricted page user_conn = get( user_conn, page_path(conn, :index) ) |> follow_link( "Restricted" ) |> assert_response( status: 200, path: session_path(conn, :new) ) |> refute_response( body: "Restricted Content" ) # admin grants the user permission get( admin_conn, page_path(conn, :index) ) |> follow_link( "Admin Dashboard" ) |> follow_form( %{ user: %{ permissoin: "ok_to_do_thing" }} ) |> assert_response( status: 200, path: admin_path(conn, :index), html: "Permission Granted" ) # the user should now be able to see the restricted page get( user_conn, page_path(conn, :index) ) |> follow_link( "Restricted" ) |> assert_response( status: 200, path: restricted_path(conn, :index), html: "Restricted Content" ) end ``` -------------------------------- ### Phoenix Integration Simulate Multiple Users Test Source: https://hexdocs.pm/phoenix_integration/PhoenixIntegration This example illustrates simulating multiple users by managing separate connection states. It shows a scenario where an admin grants permissions to a user. It uses a hypothetical `test_sign_in` function and demonstrates chaining requests for both the user and admin, asserting responses, and validating state changes. ```elixir test "admin grants a user permissions", %{conn: conn, user: user, admin: admin} do # sign in the user and admin user_conn = test_sign_in( conn, user ) admin_conn = test_sign_in( conn, admin ) # user can't see a restricted page user_conn = get( user_conn, page_path(conn, :index) ) |> follow_link( "Restricted" ) |> assert_response( status: 200, path: session_path(conn, :new) ) |> refute_response( body: "Restricted Content" ) # admin grants the user permission get( admin_conn, page_path(conn, :index) ) |> follow_link( "Admin Dashboard" ) |> follow_form( %{ user: %{ permissoin: "ok_to_do_thing" }} ) |> assert_response( status: 200, path: admin_path(conn, :index), html: "Permission Granted" ) # the user should now be able to see the restricted page get( user_conn, page_path(conn, :index) ) |> follow_link( "Restricted" ) |> assert_response( status: 200, path: restricted_path(conn, :index), html: "Restricted Content" ) end ``` -------------------------------- ### Assert Responses in Phoenix Integration Tests Source: https://hexdocs.pm/phoenix_integration/index This example demonstrates the use of `assert_response` in Phoenix integration tests to verify specific response details. It allows assertions on HTTP status codes, the expected URL path, and the HTML content of the response. This function is crucial for ensuring correct navigation and content rendering during tests. ```elixir test "Basic page flow", %{conn: conn} do get(conn, page_path(conn, :index) ) |> assert_response( status: 200, path: page_path(conn, :index), html: "Test App" ) |> follow_link( "About" ) |> assert_response( status: 200, path: about_path(conn, :index), html: "About Test App" ) |> follow_link( "Contact" ) |> assert_response( status: 200, path: about_path(conn, :contact), html: "Contact" ) |> follow_link( "Home" ) |> assert_response( status: 200, path: page_path(conn, :index), html: "Test App" ) end ``` -------------------------------- ### Phoenix Integration Create New User Test Source: https://hexdocs.pm/phoenix_integration/PhoenixIntegration This Elixir test demonstrates creating a new user using `phoenix_integration`. It navigates to a sign-up page, fills out a form with user details using `follow_form`, and then asserts the response status, path, and specific HTML content. This highlights form submission and response validation. ```elixir test "Create new user", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) # click/follow through the various about pages |> follow_link( "Sign Up" ) |> follow_form( %{ user: %{ name: "New User", email: "user@example.com", password: "test.password", confirm_password: "test.password" }} ) |> assert_response( status: 200, path: page_path(conn, :index), html: "New User" ) end ``` -------------------------------- ### Add PhoenixIntegration Dependency Source: https://hexdocs.pm/phoenix_integration/index Add `phoenix_integration` to the `deps` function in your application's `mix.exs` file. This ensures the library is available for testing. After adding, run `mix deps.get` to fetch the dependency. ```elixir defp deps do [ # ... {:phoenix_integration, "~> 0.8", only: :test} # ... ] end ``` -------------------------------- ### Create Custom IntegrationCase Source: https://hexdocs.pm/phoenix_integration/index Define a custom `IntegrationCase` module in `test/support/integration_case.ex`. This module uses `ExUnit.CaseTemplate` and includes `MyApp.ConnCase` and `PhoenixIntegration` to set up the testing environment. Alternatively, `use PhoenixIntegration` can be placed in `conn_case.ex` after the `@endpoint` definition. ```elixir defmodule MyApp.IntegrationCase do use ExUnit.CaseTemplate using do quote do use MyApp.ConnCase use PhoenixIntegration end end end ``` -------------------------------- ### Configure PhoenixIntegration Endpoint Source: https://hexdocs.pm/phoenix_integration/index This configuration step in `config/test.exs` tells `phoenix_integration` which Phoenix endpoint to use for tests. Ensure `MyApp.Endpoint` is replaced with your application's actual endpoint. Recompilation is needed if the endpoint changes. Optional `warnings: false` can disable HTML validation warnings. ```elixir config :phoenix_integration, endpoint: MyApp.Endpoint ``` -------------------------------- ### Add Phoenix Integration Dependency Source: https://hexdocs.pm/phoenix_integration/readme Add the `phoenix_integration` dependency to your application's `mix.exs` file, specifically within the `deps` function and marked for use only in the `:test` environment. After adding, run `mix deps.get` to fetch the package. ```elixir defp deps do [ # ... {:phoenix_integration, "~> 0.9", only: :test} # ... ] end ``` -------------------------------- ### Submit Form with Redirects using follow_form Source: https://hexdocs.pm/phoenix_integration/readme The `follow_form` function finds a form, fills specified fields, submits the form, and automatically follows any redirects. It is useful for chaining actions in integration tests. ```elixir test "Create new user", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) |> follow_link( "Sign Up" ) |> follow_form( %{ user: %{ name: "New User", email: "user@example.com", password: "test.password", confirm_password: "test.password" }} ) |> assert_response( status: 200, path: page_path(conn, :index), html: "New User" ) end ``` -------------------------------- ### Phoenix Integration Request Making Functions Source: https://hexdocs.pm/phoenix_integration/readme The `PhoenixIntegration.Requests` module provides functions to make requests to your Phoenix application. These functions typically search for links or forms within the HTML body of a previous response and initiate a new request. Flexibility is offered in matching links via text, href, or CSS IDs. ```elixir follow_link( conn, "About Us" ) follow_link( conn, "/about/us" ) follow_link( conn, "#about_us" ) ``` -------------------------------- ### Debug Phoenix Integration Tests with IO.inspect Source: https://hexdocs.pm/phoenix_integration/index This snippet shows how to use `IO.inspect` within a Phoenix integration test pipe chain. It helps debug by printing the current state of the connection (conn) to the console at various stages of the test flow. This requires no external dependencies beyond standard Elixir/Phoenix testing utilities. ```elixir test "Basic page flow", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) |> follow_link( "About Us" ) |> IO.inspect |> follow_link( "Home" ) |> assert_response( status: 200, path: page_path(conn, :index) ) end ``` -------------------------------- ### Refute Response Conditions using refute_response Source: https://hexdocs.pm/phoenix_integration/readme The `refute_response` function is used to assert that a response does *not* meet certain conditions. It is similar in form to `assert_response` but checks for the absence of specified criteria, often used to verify that specific content is not present. ```elixir |> follow_link( "Show Thing" ) |> assert_response( status: 200, path: thing_path(conn, :show, thing) html: "Good Content" ) |> refute_response( body: "Bad Content" ) ``` -------------------------------- ### Assert Multiple Response Conditions using assert_response Source: https://hexdocs.pm/phoenix_integration/readme The `assert_response` function allows chaining multiple conditions to assert against a response within a single call. This improves readability of integration tests by maintaining a clear chain of events. ```elixir test "Basic page flow", %{conn: conn} do # get the root index page get( conn, page_path(conn, :index) ) # click/follow through the various about pages |> follow_link( "About Us" ) |> assert_response( status: 200, path: about_path(conn, :index) ) |> follow_link( "Contact" ) |> assert_response( content_type: "text/html" ) |> follow_link( "Privacy" ) |> assert_response( html: "Privacy Policy" ) |> follow_button( "Accept" ) |> assert_response( html: "Privacy Policy" ) |> follow_link( "Home" ) |> assert_response( status: 200, path: page_path(conn, :index) ) end ``` ```elixir |> assert_response( status: 200, path: page_path(conn, :index) html: "Good Content", html: "More Content" ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.