2017-02-14 19 views
0

我是Rails的初学者,我试图用水豚编写测试功能“编辑用户”,但每次出现以下错误:使用Capybara进行功能测试:在挂钩后出现错误ActiveRecord :: RecordNotFound:

An error occurred in an after hook

ActiveRecord::RecordNotFound: Couldn't find User with 'id'=

occurred at /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 

我不知道为什么这个错误出来了,有没有关于如何排除故障的建议?

回答

0

错误告诉你什么是问题 - 你在后挂钩中有某种元素加载。在发布的任何代码中,你都不会显示任何钩子,但是堆栈跟踪应该引导你到错误产生的地方。

在测试中的其他问题是

  1. 你似乎是使用JS能够驱动(水豚,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),硬编码路径名会导致脆弱测试。而是使用提供助手的轨道来检查路径。所以,假设你已经创建的用户对象为一个名为user变量 - 这将是

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

    这样,如果你改变路线,让用户路径实际上是/成员/所有的测试不破。

+0

感谢您回复并反馈您的问题。这里是整个错误行“ActiveRecord :: RecordNotFound:找不到'id'=”的用户。这里id应该等于1,但等号后有一个空格。为什么这里是空的?我会很感激,如果你可以给你一个提示 –

+0

@AB你不会显示你创建用户(或调用访问)的位置,但它很可能是我的答案中的#1,应用程序代码无法看到用户您是由于事务性数据库测试而在测试代码中创建的,或者是因为您只是构建用户而不是创建用户。 –

相关问题