ActiveRecord :: RecordNotFound for the record just created in 'js: true' Function specification

Trying to work with javascript in a simple function specification, but I can’t 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 this entry in before(:each) and it passes when I delete js: true .

What am I missing?

+4
source share
1 answer

It looks like you are conducting tests using transactional lights. When you run your test using js: true , it will launch your application and test it in separate threads. You create your disk_file object in a transaction (which automatically rolls back at the end of the test) in the test thread. Typically, 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. database_cleaner is a popular way to do this.

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

+7
source

Source: https://habr.com/ru/post/1497313/


All Articles