使用Capybara进行功能测试:挂钩后发生错误ActiveRecord :: RecordNotFound:

时间:2017-02-14 03:44:23

标签: ruby capybara

我是Rails的初学者,我正在尝试用capybara编写功能“编辑用户”的测试,但每次我收到以下错误:

  

挂钩后发生错误

     

ActiveRecord :: RecordNotFound:找不到'id'=

的用户      

发生在/var/lib/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/core.rb:155:in`find'

这是我的测试代码:

context 'when wrong parameters' do
          scenario 'checkout name is mandatory' do
            fill_in 'name', with: ''
            fill_in 'email', with: ''
            3.times { wait_for_ajax }
            click_button '保存'
            expect(page).to have_current_path('/users/1')
            expect(page).to have_content 'お名前を入力してください'
          end

这是我的spec_helper.rb和rails_helper.rb

    require 'factory_girl_rails'

    RSpec.configure do |config|
      config.expect_with :rspec do |expectations|
        expectations.include_chain_clauses_in_custom_matcher_descriptions = true
      end

      config.mock_with :rspec do |mocks|
        mocks.verify_partial_doubles = true
      end
    end


ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'support/authentication_helper'
require 'capybara'
require 'support/wait_helper'
require 'rake'
require 'support/feature_testing_functions_helper'

Rails.application.load_tasks
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  config.include FactoryGirl::Syntax::Methods
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include AuthenticationHelper

  config.include Capybara::DSL, type: :feature
  config.include Capybara::RSpecMatchers, type: :feature
  config.include WaitHelper, type: :feature
  config.include FeatureTestingFunctions, type: :feature
  Capybara.default_driver = :webkit
  config.before :suite do
    Rake::Task['db:drop'].invoke
    Rake::Task['db:setup'].invoke
  end
  config.after :each do
    Capybara.reset_sessions!
  end
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

我不知道为什么会出现这个错误,有什么建议可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

错误告诉你问题是什么 - 你在钩子后面有一些元素加载。您没有在已发布的任何代码中显示任何后挂钩,但堆栈跟踪应该引导您到达生成错误的位置。

您的测试中的其他问题是

  1. 您似乎正在使用支持JS的驱动程序(capybara-webkit),但仍启用了事务测试。这会导致您在测试代码中创建的任何对象(FactoryGirl等)对应用程序不可见。请参阅 - https://github.com/teamcapybara/capybara#transactions-and-database-setup - 然后使用https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example中推荐的配置设置DatabaseCleaner。

  2. 停止使用wait_for_ajax - 对wait_for_ajax之类的内容有大约3种有效(非常罕见)的需求,其他任何东西都不需要或指向可怕的应用设计。如果您确实需要等待按钮才能完成某个请求,那么您应该期望页面上的任何可见更改都表明请求已完成(Capybara会自动等待/重试页面内容预期) - 如果没有可见改变用户在能够或不能点击按钮时应该知道的内容?

  3. 您不能假设您创建的对象的ID将是任何特定的数字 - (/ users / 1),如果您更改了路由名称,则硬编码路径名会导致严格的测试。而是使用rails提供的帮助程序来检查路径。假设您已将用户对象创建为名为user的变量 - 它将是

    expect(page).to have_current_path(user_path(user))
    

    这样一来,如果您改变路线以使用户路径实际上是/ member /所有测试都不会中断。

相关问题