I am using rails 4.0.5, rspec 2.14.1, capybara 2.2.1, capybara-webkit 1.1.0 and database_cleaner 1.2.0. I see some weird behavior with the following function test (which mimics a user viewing a comment on a post, hangs over an icon to display a menu, and clicking a menu item to delete a comment):
let(:user){create(:user)} let(:post){create(:post, author: user)} let!(:comment){create(:comment, post: post, author: user)} ... it "can delete a comment" do assert(page.has_css? "#comment-#{comment.id}") find("#comment-#{comment.id}-controls").trigger(:mouseover) find("#comment-#{comment.id} .comment-delete a").click assert(page.has_no_css? "#comment-#{comment.id}") end
This test fails for about 80% of the time, always because some record is retrieved from the database as nil - I get NoMethodError: undefined method X for nil:NilClass for different X values. Sometimes nil is a comment that is deleted , sometimes this is a post to which a comment is attached, sometimes it is the author of a comment / post.
If I add sleep 1 to the end of the test, it will pass:
it "can delete its own comment" do assert(page.has_css? "#comment-#{comment.id}") find("#comment-#{comment.id}-controls").trigger(:mouseover) find("#comment-#{comment.id} .comment-delete a").click assert(page.has_no_css? "#comment-#{comment.id}") sleep 1 end
It is also passed if I put sleep 1 in the after block.
Any idea why I get these NoMethodErrors and / or why the test passes if I make it sleep for a second after completing all the work?
ruby ruby-on-rails rspec capybara capybara-webkit
Dave urban
source share