0

The gist of what I'm trying to test is loading a page, updating the state of an object in the database, then reloading the page to see that reference to the object is no longer there. Something along the lines of this:

before do
  <some setup stuff that requires js: true)

  <ObjectClass>.first.update(current_state: <new state>)
  visit user_path(user)
end

it "should remove the reference to the object", js: true do
  should_not have_content "<Text about the object>"
end

In case it is relevant:

config.use_transactional_fixtures = false and the use of database_cleaner

The issue I'm running into is that often the update to the object doesn't take. If I then run the test again, with no changes anywhere, it may fail again or it may pass. I have confirmed that the state is not changing by printing it out at the end of the before, after the visit. No database errors or the like are being shown.

The object is initially being created in one of two ways...in some tests using FactoryGirl, in others using a form within the site (so via the controller). Both types of tests are displaying this behavior.

Thoughts as to what might be causing this intermittent behavior?

Additional note:

Here's the contents of spec/support/database_cleaner.rb, which was likely some default version that I'm inspecting more closely right now:

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end
uhezay
  • 341
  • 1
  • 2
  • 11
  • 1
    possible duplicate of [Why does adding "sleep 1" in an after hook cause this Rspec/Capybara test to pass?](http://stackoverflow.com/questions/23554882/why-does-adding-sleep-1-in-an-after-hook-cause-this-rspec-capybara-test-to-pas) – Dave Schweisguth Aug 23 '14 at 15:17
  • 1
    Thanks for validating that wait_until is appropriate in some ajax-heavy/database update cases, I've only been reading blog posts and answers that claim the built in Capybara wait should be sufficient under all circumstances and been banging my head against a wall trying to make it work for many, many failing test cases. All green now! – uhezay Aug 26 '14 at 00:35
  • Should I mark this question as answered somehow? – uhezay Aug 26 '14 at 00:36
  • No, if it's a dupe you can either just leave it be (if you think your statement of the question is different enough from the question it's a dupe of that it might help lead someone to that other question, which I think is the case) or delete it (if it's not providing any additional value on the site). And you can vote up that other question and answer, which I think you did already. – Dave Schweisguth Aug 26 '14 at 16:30

0 Answers0