ruby on rails - Rspec giving false negative whilst testing pagination -


i'm writing request spec, test will_paginate working ok, , i've got few problems. firstly, here's pruned version of spec:

require 'spec_helper'  describe "articles"    subject { page }    describe "index page"      let(:user) { factorygirl.create(:user) }      before { visit news_path }      describe "pagination"        before(:all) { 31.times { factorygirl.create(:article, user: user) } }       after(:all) { article.delete_all; user.delete_all }        let(:first_page)  { article.paginate(page: 1) }       let(:second_page) { article.paginate(page: 2) }        "should not list second page of articles"         second_page.each |article|           page.should_not have_selector('li', text: article.title)         end       end     end   end end 

as can see there test ensure second page of articles not shown when user visits articles index page. test fails:

1) articles index page pagination should not list second page of articles    failure/error: page.should_not have_selector('li', text: article.title)    expected css "li" text "article number 1" not return 

i can't understand why failing. when manually create 31 articles, in development env, , view in browser, pagination works fine, when switch test env, specs fail.

article model:

class article < activerecord::base   attr_accessible :body, :title   belongs_to :user    validates :user_id, presence: true    default_scope order: 'created_at desc' end 

article factory looks this:

factorygirl.define   factory :article     sequence(:title)  { |n| "article number #{n}" }     body "this body"     user   end end 

quite incredibly, solution following;

change:

before(:all) { 31.times { factorygirl.create(:article, user: user) } } 

to:

before   31.times { factorygirl.create(:article, user: user) }   visit news_path end 

two things learned here:

  1. the before block must not target (:all), otherwise tests fail
  2. i need explicitly run visit news_path inside before block, after creation of factories, otherwise capybara's page object not expect be

so, illustrate:

this won't work:

# fails because targets (:all) before(:all)   31.times { factorygirl.create(:article, user: user) }   visit news_path end 

and neither this:

# fails because visiting news path before objects exist before   visit news_path   31.times { factorygirl.create(:article, user: user) } end 

it needs this:

# not targeting (:all) , visiting news path after creation of objects before   31.times { factorygirl.create(:article, user: user) }   visit news_path end 

over 20 hours figure out, @ least learned new stuff etc.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -