Programming in ruby or rials – ror: Capybara doesn’t follow redirects and changes database on newest questions tagged ruby-on-rails – Stack Overflow

I have two failing specs that seem to be somehow related. Here are they:

describe "exclude from navigation button" do
  before do
      within "#page_#{pages[1].id}" do
      click_link I18n.t 'pages.exclude'
    end
    pages[1].reload
  end
  specify { pages[1].menu_order.should == -1 }
  it "should add an excluded css class to the pages control" do
    page.should have_selector "li.excluded#page_#{pages[1].id}"
  end
end

describe "include in navigation button" do
  before do
    within "#page_#{pages[2].id}" do
      click_link I18n.t 'pages.include'
    end
    pages[2].reload
  end
  specify { pages[2].menu_order.should == 1 }
  it "should remove the excluded css class from the pages control" do
    page.should_not have_selector "li.excluded#page_#{pages[2].id}"
  end
end

Both describe blocks fail when asserting the css (second example of each block) with expected css "..." (not) to return (any|some)thing.

This is the controller code of the click_link actions:

def exclude_page
  page = Page.find params[:page_id]
  Hierarchy::PageHierarchy.exclude! page
  redirect_to admin_pages_path
end

def include_page
  page = Page.find params[:page_id]
  Hierarchy::PageHierarchy.include! page
  redirect_to admin_pages_path
end

These are the modules methods exclude! and include!:

def self.exclude! page
  page.update_attribute :menu_order, -1
end

def self.include! page
  page.update_attribute :menu_order, 1
  menu_order = 2
  page.siblings.each do |p|
    p.update_attribute :menu_order, menu_order unless p.menu_order == -1
    menu_order += 1
  end
end

And this is the controller code of the target of the redirect (admin_pages_path):

def index
  @pages = Page.roots
end

Where Page.roots is a scope of where parent_id: nil.

The first example block is easy to fix. I just need to revisit the page and the CSS matches. But afaik Capybara should just follow the redirect. So why do I have to revisit?

The second block is more difficult (just punch me if this is worth its own question, I just wanted to avoid doubleposting all the code.)

Revisiting the page will not do the trick this time. In fact revisiting seems to change the database:

    69:       end
    70:       pages[2].reload
    71:     end
    72:     its(:menu_order) { should == 1}
    73:     it "should add an excluded css class to the pages control" do
 => 74:       binding.pry
    75:       page.should_not have_selector "li.excluded#page_#{pages[2].id}"
    76:     end
    77:   end
    78: end

[1] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_2>)> pages[2].id
=> 9
[2] ... > Page.find(9).menu_order
=> 1
[3] ... > visit admin_pages_path
=> nil
[4] ... > Page.find(9).menu_order
=> -1

This is the point where I get confused. So what I dont understand is:
- Why do I have to revisit in the first example block?
- Why does this trick not work in second example block?
- Why is revisiting the page changing the database?

Note that all of this only applys for Capybara/RSpec. When doing this manually with the browser, everything works fine.

UPDATE:

Definition of pages:

let(:pages) { bunch_of_pages.sort { |a, b| a.menu_order <=> b.menu_order } }

# bunch_of_pages:
def bunch_of_pages
  pages = []
  roots << root1 = FactoryGirl.create(:public_page, menu_order: (rand*1000).to_i)
  child1 = FactoryGirl.create :public_page, parent: root1, menu_order: (rand*1000).to_i
  child2 = FactoryGirl.create :public_page, parent: root1, menu_order: (rand*1000).to_i
  subchild1 = FactoryGirl.create :public_page, parent: child2, menu_order: (rand*1000).to_i

  pages << root2 = FactoryGirl.create(:public_page, menu_order: (rand*1000).to_i)
  child3 = FactoryGirl.create :public_page, parent: root2, menu_order: (rand*1000).to_i
  .
  .
  .

  pages
end

See Answers


source: http://stackoverflow.com/questions/11702488/capybara-doesnt-follow-redirects-and-changes-database
Programming in ruby or rials – ror: programming-in-ruby-or-rials-ror



online applications demo