简体   繁体   中英

Integration test: `assert_select 'a[href=?]'` fails for paginated pages

I have many integration tests that look something like:

first_page_of_users = User.page(1)                    # Using kaminari for pagination.
first_page_of_users.each do |user|
  assert_select 'a[href=?]', user_path(user)
end

These tests fail with an error message such as:

Expected at least 1 element matching "a[href="/users/1"]", found 0..

I added puts @response.body to the tests to see the code that it responded with.

For all the users included in the response body it has correct hrefs, such as href="/users/75938613" . However, the numbers behind /users/ are much higher, and "users1" is indeed not present (not just the url not present but the entire record isn't on that page). If I ensure that there are only a few fixtures so that no pagination applies, the tests pass. This makes me feel as if with pagination the order that first_page_of_users = User.page(1) applies differs from the order in records that first_page_of_users.each do |user| assert_select 'a[href=?]', user_path(user) expects...

What could be causing this error? It applies to different pages and models of my app.


Update: For another model even when I try

Organization.all.each do |org|
  puts @response.body
  assert_select 'a[href=?]', organization_path(org)
end

I get the error:

Expected at least 1 element matching "a[href="/organizations/fix0"]",

The organization with the name "fix0" is not present at all in the body that it reponds with. My (shortened) fixtures are below and they confirm there should not be any organization with that name. Any idea what's going on?

one:
  name: abcd
  activated: true
  activated_at: 2015-04-16 11:38:53

two:
  name: efgh
  activated: true
  activated_at: 2015-04-16 11:38:53

<% 35.times do |n| %>
organization_<%= n %>:
  name: <%="fix#{n}" %>
  activated: true
  activated_at: <%= Time.zone.now %>
<% end %>

When integration testing, it's important to keep track of what should show up where and test just that. Because we have too many records for the first page in this case, anything that would land on later pages will cause the failing test. As you suspect, something like this should verify that the first page of results is present:

Organization.order('organizations.name','desc').page(1).each do |organization|
     assert_select 'a[href=?]', organization_path(organization)
end

I recommend keeping a smaller data set for testing, because in this case you should also 'get' page two in your test and do the assertions above for 'page(2)' as well. This means this integration test has to issue two get requests (plus any setup you may need) and check the results, and making a habit of that can slow down your suite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM