这样做迈克尔·哈特尔的Rails的教程,在第10章,第5节,锻炼2 。 我 - 很干脆 - 试图写的测试,以确保出现在使用will_paginate gem是几页分页格(这似乎是哈特尔的首选测试是否分页作品的方法),但是当我添加以下代码..
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
格存在于我的浏览器的源代码,当然,它的正常工作。
因为在一些地方,而不是其他人失败了,我得假设这是莫名其妙地“范围” -或分配型问题相关will_paginate
-你看,对于失败的测试,而分页内容实际上是一部分“微柱”的控制器,但网页/视图被测试由被处理‘用户’控制器。 对于通过的测试,视图和控制器都为“用户”。
莫非是这个问题? 这也有可能是我的FactoryGirl设置/调用被打破,而不是触发测试分页代码的某些原因。 我是一个Rails的n00b - 实际上完全新的节目 - 所以感谢你们大家。 :)
(也,PS,我怎么做我的代码丰富多彩,漂亮的SO?)
/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
编辑:发现了这篇文章,will_paginate和查看相关的测试 ,但不明白它老老实实(可能是由于语法)..难道是某种联系?