ActiveRecord :: RecordNotFound用于在'js:true'功能规范中创建的记录

Mel*_*emi 4 rspec ruby-on-rails capybara selenium-webdriver

尝试在简单的功能规范中使用javascript,但我甚至无法访问该页面.

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
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

  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'
Run Code Online (Sandbox Code Playgroud)

这让我感到困惑,因为我刚刚创建了该记录,before(:each) 在删除时通过了js: true.

我错过了什么?

Ste*_*eve 7

看起来您正在使用事务夹具运行测试.当您运行测试时js: true,它将运行您的应用程序并在单独的线程中进行测试.您可以disk_file在测试线程中的事务中创建对象(在测试结束时自动回滚).通常,除非提交(不会发生),否则您的应用程序将无法看到此事务中所做的更改.

此问题的解决方案是为JavaScript测试使用不同的数据库清理策略.该database_cleaner宝石是这样做流行方式.

另请参见https://github.com/jnicklas/capybara#transactions-and-database-setup