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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With