在chapter 10, section 5, exercise 2上做Michael Hartl的Rails教程。我 - 非常简单 - 尝试编写测试以确保使用will_paginate gem在几页上出现分页div(这似乎是Hartl测试分页是否有效的首选方法),但是当我添加以下代码时.. < / p>
subject { page }
.
.
.
it { should have_selector('div.pagination') }
..它返回..
1) UserPages profile page
Failure/Error: it { should have_selector('div.pagination') }
expected css "div.pagination" to return something
这特别奇怪,因为在这个特定的_spec文件中,这个相同的Rspec代码在一些测试中传递而在其他测试中失败(我将在下面强调这一点)。此外,pagination
div存在于浏览器的源代码中,当然它工作正常。
因为它在某些地方而不是其他地方失败了,我得假设这是某种“范围” - 或与will_paginate
相关的分配类型问题 - 你看,对于失败的测试,内容是paginated实际上是“Microposts”控制器的一部分,但正在测试的页面/视图由“Users”控制器处理。对于通过测试,视图和控制器都是“用户”。
这可能是问题吗?我的FactoryGirl设置/调用也可能被破坏,并且由于某种原因没有在测试中触发分页代码。我是一个Rails n00b - 实际上是编程的新手 - 所以谢谢你们。 :)
(另外,p.s.,如何让我的代码变得丰富多彩而美观?)
/spec/requests/user_pages_spec.rb
require 'spec_helper' # Omitted from below - I don't think this is relevant.
describe "UserPages" do
subject { page }
describe "index" do # This is the block where it is PASSING..
let(:user) { FactoryGirl.create(:user) } # I'll include my /spec/factories.rb file below
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all } # I'll omit my /app/models/user.rb file - I don't think it's relevant to the problem
before do
valid_signin user # References /spec/support/utilities.rb to sign in as a valid user, but I will omit this since this is part of the working tests
visit users_path # This is /app/views/users/index.html.erb - this is where SUCCESSFUL pagination testing occurs
end
describe "pagination" do
it { should have_selector('div.pagination') } #PASS!! As I would expect
.
.
.
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Awesome")} # Used for other tests
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Sauce") } # Used for other tests
before(:all) { 30.times { FactoryGirl.create(:micropost) } } # Is this constructed properly? Perhaps the posts are not created, and that's why the failure to render the paginate code in test.
after(:all) { user.microposts.delete_all } # I have tested with and without this line of code - it fails both ways.
before { visit user_path(user) } # /app/views/users/show.html.erb
it { should have_selector('div.pagination') } # FAIL!!! This is the line in question that generates the error above.
.
.
.
.
end
/app/views/users/index.html.erb (分页在网站上工作,也在测试工作)
<%= provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
/app/views/users/show.html.erb (分页在网站上工作,测试失败)
<% provide(:title, @user.name) %>
<div class="row">
.
.
.
<div class="span8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %> # Here is where I declare that pagination should occur on @microposts, NOT @users
<% end %>
</div>
</div>
/spec/requests/factories.rb
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com" }
password "secret"
password_confirmation "secret"
factory :admin do
admin true
end
end
factory :micropost do
content "Lorem ipsum"
user
end
end
/ Gemfile中
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'pg', '0.12.2'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
group :development do
gem 'guard-rspec', '0.5.5'
gem 'annotate', '~> 2.4.1.beta'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test, :development do
gem 'rspec-rails', '2.9.0'
end
group :test do
gem 'capybara', '1.1.2'
gem 'rb-fsevent', '0.4.3.1', :require => false
gem 'growl', '1.0.3'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'factory_girl_rails', '1.4.0'
end
编辑:找到了this article related to will_paginate and view testing,但是老老实实地不理解(可能是由于语法原因)..它可能以某种方式相关吗?
答案 0 :(得分:5)
使用时:
before(:all) { 30.times { FactoryGirl.create(:user) } }
您已经创建了一个用户,因此总计数最多为31,因此它分页(默认情况下,使用will_paginate分页显示每页30条记录)。
尝试创建 31 微博而不是30,它应该通过。
before(:each) { 31.times { FactoryGirl.create(:micropost, user: user) } }
编辑:我忘了将用户传递给微博工厂,它现在应该可以工作了(我的测试中的代码是通过的)
答案 1 :(得分:1)
根据http://thewebfellas.com/blog/2008/8/3/roll-your-own-pagination-links-with-will_paginate
"<%= will_paginate %>
“生成一个带有class = pagination
的div标签,但我没有看到任何暗示对'div.pagination'
通行证进行测试的内容。
我将测试更改为
it { should have_selector('div', class: 'pagination') }
那应该基本上测试相同的东西,这对我而言。希望这有帮助!
答案 2 :(得分:0)
<%= provide (:title, 'All users') %>
<h1>All users</h1>
<div class="pagination">
<%=will_paginate%>
</div>
<ul class="users">
<%=render @users%>
</ul>
<div class="pagination">
<%= will_paginate %>
</div>