簡體   English   中英

使用Capybara / Rspec使用will_paginate Gem測試視圖

[英]Testing views with will_paginate Gem using Capybara/Rspec

《執行Michael Hartl的Rails教程》, 第10章,第5節,練習2 我-非常簡單-嘗試編寫測試以確保使用will_paginate gem(這似乎是Hartl測試分頁是否有效的首選方法)在幾個頁面上顯示分頁div,但是當我添加以下代碼時。

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有關的“范圍”或分配類型問題-您會看到,對於失敗的測試,被分頁的內容實際上是“ Microposts”控制器,但是要測試的頁面/視圖由“ Users”控制器處理。 對於通過的測試,視圖和控制器都是“用戶”。

這可能是問題嗎? 也有可能我的FactoryGirl設置/調用被破壞,並且由於某種原因未觸發測試中的分頁代碼。 我是Rails n00b-實際上是編程新手-所以謝謝。 :)

(此外,ps,如何使我的代碼豐富多彩且如此漂亮?)

/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和視圖測試有關 ,但不誠實地理解它(可能是由於語法)..可能以某種方式相關嗎?

使用時:

before(:all)  { 30.times { FactoryGirl.create(:user) } }

您已經創建了一個用戶,因此總數達到31,因此可以分頁(默認情況下,使用will_paginate進行分頁每頁顯示30條記錄)。

嘗試創建31個微博,而不是30個,它應該會通過。

before(:each) { 31.times { FactoryGirl.create(:micropost, user: user) } }

編輯:我忘了將:user傳遞給micropost工廠,它現在應該可以工作(是我在測試中擁有的代碼,它可以通過)

根據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') }

基本上應該測試同一件事,這對我來說還是正確的。 希望這可以幫助!

<%= 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM