4

I'm new in rails. In my project i use rspec + capybara + poltergeist + selenium + database_cleaner.

Source code github link

I have postgresql 9.5.5, ubuntu 16.04 LTS.

When run test

rspec spec/controllers # everything work fine

When run

rspec spec/features # everything work fine too

But when i run all tests

rspec # part of tests fail

When run first acceptance test with :selenium -> question_id in browser 28, but must be 1, because it use database_cleaner.

Why database_cleaner not clean my database? What i do wrong? I spent a day to find a solution, but not found nothing. Help me please.

P.S. It's a training project.

My Database_Cleaner configuration is:

  config.use_transactional_fixtures = false

  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
    Warden.test_reset!
    if Rails.env.test?
      FileUtils.rm_rf(Dir["#{Rails.root}/public/uploads"])
    end
  end
Bozhkov A.
  • 67
  • 1
  • 8
  • 1
    Why do you think in must be 1 ? Depending on how you have setup database_cleaner it's possible for the database to be empty but not reset the id column counter -- Have you queried the database to see how many questions are saved in it? Your tests should not have any hardcoded record ids in them. If that's not the issue then add one of your tests and your database_cleaner configuration to the your question. – Thomas Walpole Feb 25 '17 at 18:32
  • I add the files to question. And by default i think, that question in db is one, and link to added file must have link href: '/uploads/attachment/file/1/test_file.dat', where 1 is question id. But in reality question id is 28. I thought database cleaner clean tables each time when test runs. – Bozhkov A. Feb 25 '17 at 18:37
  • 1
    @ThomasWalpole - i'm understand what about you talk. Yes, table have just 1 row. I need to rewrite my tests. Thank you for solution. – Bozhkov A. Feb 25 '17 at 18:56
  • Ok -- I was looking in the wrong branch - I see the tests you're talking about now - updating my answer – Thomas Walpole Feb 25 '17 at 18:57

2 Answers2

4

Cleaning the tables means it deletes all records, it doesn't necessarily mean it resets the index counters - just like if you delete record 3 and then add a new record - the new one would be 4. So in your case it's possible 27 records have been deleted and the next one you create would be 28 even though there's only 1 actual record. Query the database to see how many actual records are there to verify that.

Next, your database cleaner config should be more like the recommended config - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example . append_after vs after is important for stability and checking the driver name vs just the :js metadata is too.

When checking for the href '/uploads/attachment/file/1/test_file.dat' in your tests you shouldn't be checking for a hardcoded '1' there you should check based on the record id number of the record you created. So "/uploads/attachment/file/#{question.id}/test_file.dat" (obviously question.id will depend on what objects you've created and your variable names, but that matches at least one of your tests I think.

Additionally after a quick look at your specs - anywhere you're doing expect(current_path).to eq ... is wrong. You should be doing expect(page).to have_current_path(...). The first way disables the retrying behavior of Capybara and will lead to flaky tests.

And finally - where you have expect(page).not_to have_content t('common.button.ready'), I'm guessing that should be expect(page).not_to have_button t('common.button.ready')

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
0

Here you could read how to config a database_cleaner with RSpec and Capybara - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example

bgs
  • 161
  • 5