Monday, 15 August 2011

ruby on rails - Why does this requests spec fail intermittently, or how can I debug it? -



ruby on rails - Why does this requests spec fail intermittently, or how can I debug it? -

this test fails intermittently:

context "as regular_user" before :each @user = factorygirl.create(:user, :role => 'regular_user') visit new_user_session_path unless current_path == new_user_session_path fill_in "email", :with => @user.email fill_in "password", :with => @user.password click_button "sign in" end "removes thing favorites while on thing index page" @things = factorygirl.create_list(:thing, 5) @user.set_mark :favorite, @things.first @user.reload.favorite_things visit things_path unless current_path == things_path expect{ first(:link, "remove favorites").click # fails @user.reload.favorite_things }.to alter { @user.favorite_things.count }.by(-1) page.should have_content("sort by") end end

i'm using gem markable provide favorites feature.

there doesn't seem pattern success/failure when run specs rspec (it's not 1 time fails fails, or vice versa). there might pattern when specs run through guard (spork beingness used) in if start guard , test passes pass while instance of guard running ... sames goes failures - if start guard , fails, subsequent runs within instance of guard fail.

just ran rspec spec/requests/users_spec.rb:148 run 1 spec, , failed:

1) users regular_user removes thing favorites while on thing index page failure/error: click_link "remove favorites" # fails capybara::elementnotfound: unable find link "remove favorites" # ./spec/requests/users_spec.rb:155:in `block (4 levels) in <top (required)>' # ./spec/requests/users_spec.rb:153:in `block (3 levels) in <top (required)>'

i ran rspec spec/requests/users_spec.rb:148 1 time again right after that, , succeeded:

finished in 0.83754 seconds 1 example, 0 failures

i've tried adding "asdfasdf" within <% @things.each |thing| %> section of index.html.erb view file, , checking page.should have_content("asdfasdf") , fails sometimes. tried adding page.should have_selector('.favoritesblock', visible: true) before expect block (favoritesblock exists within same @things block in view), , fails sometimes. never fails find text outside of <% @things.each |thing| %> loop, however.

i added save_and_open_page before first(:link, "remove favorites").click # fails line, , able produce instance spec failed. page contained 2 things (my factorygirl phone call should creating 5) , neither of them had 'remove favorites' link (depending on if thing favorited or not, displays "add favorites" or "remove favorites").

based on that, tried modifying first 2 lines of spec bit, didn't help (still fails sometimes):

5.times { factorygirl.create(:thing) } @user.set_mark :favorite, thing.first

what did notice when succeeds, display 'remove favorites' link first thing doesn't display 5 things.

so, need know why spec fails intermittently or else can figure out what's causing problem. ideas?

the way set mill cause of intermittent rspec failures.

the item has boolean field, active, either displays thing on site (if it's true) or not display thing on site (if it's false).

i clever factory, , set field f.active { [true, false].sample }. so, mill creates active things , creates inactive things. purposefully created factories behave way because want default mill create wide selection of possible real-world things possible. need maintain in mind aspects need manually set - in case, need create active things.

the prepare alter spec create active things:

@things = factorygirl.create_list(:thing, 5, :active => true)

ruby-on-rails capybara factory-girl rspec-rails

No comments:

Post a Comment