Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: accept a list of strings in assert_response(html: ...) #51

Open
jaimeiniesta opened this issue Apr 30, 2021 · 1 comment

Comments

@jaimeiniesta
Copy link

Hello!

I want to test a blog that paginates posts, showing 5 per page. I'd like to get the post list, and check that I can find the post title of the first 10 posts, something like this:

test "Blog post pagination", %{conn: conn} do
  get(conn, "/blog")
  |> assert_response(status, 200, path: "/blog", html: "Listing recent posts")
  |> assert_response(html: "Post title 1")
  |> assert_response(html: "Post title 2")
  |> assert_response(html: "Post title 3")
  |> assert_response(html: "Post title 4")
  |> assert_response(html: "Post title 5")
end

Instead of this, it would be great if we could pass a list of strings to :html, so we could do this:

test "Blog post pagination", %{conn: conn, posts: posts} do
  get(conn, "/blog")
  |> assert_response(status, 200, path: "/blog", html: "Listing recent posts")
  |> assert_response(html: Enum.map(posts, & &1.title))
end

Or this, check for different strings explicitly:

test "Pricing plans", %{conn: conn} do
  get(conn, "/pricing")
  |> assert_response(status, 200,
    path: "/pricing",
    html: [
      "Pricing",
      "Starter plan",
      "Pro plan",
      "Enterprise plan"
    ]
  )
end

Same for refute_response.

What do you think?

@florish
Copy link

florish commented Dec 10, 2021

@jaimeiniesta Hi, just a quick tip to achieve almost the same with the current features: use html: multiple times. This is explicitly allowed per the documentation:

Conditions can be used multiple times within a single call to assert_response. This can be useful to look for multiple text strings in the body.

Your first example then looks like this:

test "Blog post pagination", %{conn: conn} do
  get(conn, "/blog")
  |> assert_response(
    status: 200, 
    path: "/blog", 
    html: "Listing recent posts",
    html: "Post title 1",
    html: "Post title 2",
    html: "Post title 3",
    html: "Post title 4",
    html: "Post title 5"
  )
end

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants