Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::RecordNotFound for record just created in 'js: true' Feature spec

Trying to work with javascript in a simple feature spec but I'm not able to even visit the page.

describe "DiskFiles" do
  ...
  describe "update action", js: true do
    before(:each) do
      df = FactoryGirl.create(:disk_file)
      visit edit_disk_file_path(df)
    end

    it "should not show checkmark if no match" do
      expect(page).to_not have_content "✓"
    end
  end
end

I get this error:

  1) DiskFiles update action should not show checkmark if no match
     Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::RecordNotFound:
       Couldn't find DiskFile with id=7598 [WHERE "disk_items"."type" IN ('DiskFile')]
     # ./app/controllers/disk_files_controller.rb:89:in `set_disk_file'

Which confuses me because I just created that record in before(:each) and it passes when I remove js: true.

What am I missing?

like image 501
Meltemi Avatar asked Aug 15 '13 19:08

Meltemi


1 Answers

It looks like you are running your tests with transactional fixtures. When you run your test with js: true it will run your application and test in separate threads. You create your disk_file object in a transaction (that is automatically rolled back at the end of the test) in the test thread. Normally changes made in this transaction will not be visible to your application unless they are committed (which will not happen).

The solution to this problem is to use a different database cleanup strategy for your JavaScript tests. The database_cleaner gem is popular way to do this.

See also https://github.com/jnicklas/capybara#transactions-and-database-setup

like image 124
Steve Avatar answered Nov 10 '22 05:11

Steve