Ruby on rails 使用Factory Girl关联的Rspec功能规范
我正在尝试为需要编辑其帐户模型设置的用户测试功能规范。我是测试新手,所以不确定我的问题是由于我如何设置Factory girl关联,还是因为我的数据库清理器配置有问题Ruby on rails 使用Factory Girl关联的Rspec功能规范,ruby-on-rails,ruby,rspec,factory-bot,database-cleaner,Ruby On Rails,Ruby,Rspec,Factory Bot,Database Cleaner,我正在尝试为需要编辑其帐户模型设置的用户测试功能规范。我是测试新手,所以不确定我的问题是由于我如何设置Factory girl关联,还是因为我的数据库清理器配置有问题 FactoryGirl.define do factory :user do first_name "John" last_name "Smith" sequence(:email) { |n| "John#{n}@example.com" } password "pw" end end
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Smith"
sequence(:email) { |n| "John#{n}@example.com" }
password "pw"
end
end
FactoryGirl.define do
factory :account do
association :owner, factory: :user
name "Account One"
end
end
My spec_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'shoulda/matchers'
require 'database_cleaner'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = 'random'
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, type: :controller
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = OmniAuth::AuthHash.new({
:provider => 'twitter',
:uid => '12345'
})
OmniAuth.config.add_mock(:google, {:uid => '12345'})
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
我的规格/功能/帐户\u规格rb
require 'spec_helper'
feature 'Account' do
before :each do
@account = create(:account)
@user = @account.owner
sign_in
end
scenario 'a signed in user updates the account settings' do
expect(current_path).to eq edit_account_path(@account.id)
expect(page).to have_content('Edit Account')
fill_in 'Company Name', with: 'New XYZ Co.'
click_button 'Update Account'
expect(page).to have_content('Settings were successfully updated.')
end
scenario 'a signed in user receives an error message when deletes company name' do
fill_in 'Company Name', with: nil
click_button 'Update Account'
expect(page).to have_content("Can't be blank")
end
def sign_in
visit root_path
click_link 'Sign in'
fill_in 'Email', with: @user.email
fill_in 'Password', with: @user.password
click_button 'Sign in'
click_link 'Account'
end
end
如果我只运行一个规范,我就能通过测试:
Account
a signed in user updates the account settings
a signed in user receives an error message when deletes company name
Finished in 1.71 seconds
2 examples, 0 failures
但是,当我运行整个测试套件时,会出现错误:
1) Account a signed in user updates the account settings
Failure/Error: expect(current_path).to eq edit_account_path(@account.id)
expected: "/accounts/11/edit"
got: "/accounts/33/edit"
(compared using ==)
# ./spec/features/account_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 6.85 seconds
88 examples, 1 failure, 7 pending
Failed examples:
rspec ./spec/features/account_spec.rb:10 # Account a signed in user updates the account settings
1)帐户登录用户更新帐户设置
失败/错误:预期(当前路径)。编辑帐户路径(@account.id)
应为:“/accounts/11/edit”
得到:“/accounts/33/edit”
(使用==进行比较)
#./spec/features/account_spec.rb:11:in'block(2层)in'
以6.85秒完成
88个示例,1个失败,7个待定
失败的示例:
rspec./spec/features/account_spec.rb:10#帐户登录用户更新帐户设置
出于好奇,尝试将@user=@account.owner
更改为@user=create(:user,owner:@account)
当进行此更改时,获取35;的未定义方法“owner=”,我使用的代码基于本文中的关联。在某个测试之后,您的会话有没有可能没有被重置?让一个老用户登录?尝试在:each之后添加一个,以注销用户并查看发生了什么情况Hey Steve-我认为问题在于factorygirl中的延迟加载。将before
块替换为:let(:account){create(:account)}
let(:user){account.owner}
before(:each){sign_in}
@Steve您使用的数据库清理器的哪个版本?