集成测试:`assert_select'a [href =?]'`对于分页的页面失败 [英] Integration test: `assert_select 'a[href=?]'` fails for paginated pages

查看:227
本文介绍了集成测试:`assert_select'a [href =?]'`对于分页的页面失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多集成测试,如下所示:

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..

我在测试中添加了puts @response.body,以查看其响应的代码.

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

对于响应正文中包含的所有用户,它具有正确的href,例如href="/users/75938613".但是,/users/后面的数字要高得多,并且确实不存在"users1"(不仅不存在url,而且整个记录也不在该页面上). 如果我确保只有少数固定装置以致不应用分页,则测试通过.这使我觉得first_page_of_users = User.page(1)的应用顺序与first_page_of_users.each do |user| assert_select 'a[href=?]', user_path(user)期望...

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

我得到了错误:

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

在其回复的正文中根本没有名称为"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

我建议保留较小的数据集进行测试,因为在这种情况下,您还应该在测试中获取"第二页,并对"page(2)"执行上述声明.这意味着此集成测试必须发出两个get请求(以及您可能需要的任何设置)并检查结果,并养成一个习惯,这可能会降低套件的速度.

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.

这篇关于集成测试:`assert_select'a [href =?]'`对于分页的页面失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆