我是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
我不知道为什么这个错误出来了,有没有关于如何排除故障的建议?
感谢您回复并反馈您的问题。这里是整个错误行“ActiveRecord :: RecordNotFound:找不到'id'=”的用户。这里id应该等于1,但等号后有一个空格。为什么这里是空的?我会很感激,如果你可以给你一个提示 –
@AB你不会显示你创建用户(或调用访问)的位置,但它很可能是我的答案中的#1,应用程序代码无法看到用户您是由于事务性数据库测试而在测试代码中创建的,或者是因为您只是构建用户而不是创建用户。 –