gpt4 book ai didi

ruby-on-rails - 我无法使用 FactoryGirl.create 创建对象,验证失败用户已被占用

转载 作者:行者123 更新时间:2023-12-04 05:47:33 25 4
gpt4 key购买 nike

我正在尝试使用 gem FactoryGirl 创建平衡对象,但是当我打开 Rails 控制台并编写命令 FactoryGirl.create(:balance) 时,出现错误。

我在我的模型之间创建了关联 has_one 和belong_to,此外还添加了对外部字段中唯一键的验证,这些是我的模型。

平衡

class Balance < ActiveRecord::Base
#association macros
belongs_to :user
belongs_to :wallet

#validation macros
validates :user_id, presence: true, uniqueness: true
validates :wallet_id, presence: true, uniqueness: true
end

钱包
class Wallet < ActiveRecord::Base
#association macros
belongs_to :user
belongs_to :wallet_type
has_one :bank_account, dependent: :destroy
has_one :debit_card, dependent: :destroy
has_one :credit_card, dependent: :destroy
has_one :balance, dependent: :destroy

#validation macros
validates :user_id, presence: true
validates :wallet_type_id, presence: true

end

用户
class User < ActiveRecord::Base
#association macros
...
has_one :salt
has_one :balance

end

工厂平衡
FactoryGirl.define do
factory :balance do
user
wallet
amount 2000
end
end

我的平衡测试
RSpec.describe @Balance, type: :model do

describe "Balance" do

before(:each) do
@user = FactoryGirl.create(:user)
@wallet = FactoryGirl.create(:wallet)
@balance = FactoryGirl.create(:balance, user_id: @user[:id], wallet_id: @wallet[:id])
end

it "when is valid" do
expect(@balance).to be_valid
end

it "when is presence user_id" do
expect(@balance).to validate_presence_of :user_id
end

it "when is presence wallet_id" do
expect(@balance).to validate_presence_of :wallet_id
end

pending "when is uniqueness user_id" do
expect(@balance).to validate_uniqueness_of :user_id
end

pending "when is uniqueness wallet_id" do
expect(@balance).to validate_uniqueness_of :wallet_id
end

it "when is belongs_to :user" do
should belong_to(:user)
end

it "when is belongs_to :wallet" do
should belong_to(:wallet)
end

end

end

当我创建平衡对象时 FactoryGirl.create(:balance)我收到以下错误。
ActiveRecord::RecordInvalid: Validation failed: User has already been taken
from .../lib/active_record/validations.rb:79:in `raise_record_invalid'
from .../lib/active_record/validations.rb:43:in `save!'
from .../lib/active_record/attribute_methods/dirty.rb:29:in `save!'
from .../lib/active_record/transactions.rb:291:in `block in save!'
from .../lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
from .../lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
from .../lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
from .../lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from .../lib/active_record/transactions.rb:220:in `transaction'
from .../lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
from .../lib/active_record/transactions.rb:291:in `save!'
from .../lib/factory_girl/configuration.rb:14:in `block in initialize'
from .../lib/factory_girl/evaluation.rb:15:in `[]'
from .../lib/factory_girl/evaluation.rb:15:in `create'
from .../lib/factory_girl/strategy/create.rb:12:in `block in result'
from .../lib/factory_girl/strategy/create.rb:9:in `tap'
from .../lib/factory_girl/strategy/create.rb:9:in `result'
from .../lib/factory_girl/factory.rb:42:in `run'
from .../lib/factory_girl/factory_runner.rb:23:in `block in run'
from .../lib/active_support/notifications.rb:166:in `instrument'
from .../lib/factory_girl/factory_runner.rb:22:in `run'
from .../lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
from (irb):1
from .../lib/rails/commands/console.rb:110:in `start'
from .../lib/rails/commands/console.rb:9:in `start'
from .../lib/rails/commands/commands_tasks.rb:68:in `console'
from .../lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from ..lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'irb(main):002:0> a= FactoryGirl.create(:balance)

最佳答案

问题

运行测试时会看到此错误:

ActiveRecord::RecordInvalid: Validation failed: User has already been taken

因为您已经在之前的任何测试运行中创建了该用户。所以,它得到了那个验证错误。

解决方案

您可以使用 database_cleaner gem这将确保您运行测试的测试数据库处于干净状态。

您可以通过将此添加到您的 Gemfile 来安装 gem。 :
group :test do
gem 'database_cleaner'
end

然后运行:
bundle install

之后,配置 database_cleaner在您的 spec_helper.rb :
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
DatabaseCleaner.start
end

config.after(:each) do
DatabaseCleaner.clean
end
end

此配置将确保在每个规范之后截断上次测试运行的数据。而且,每次运行新规范时,都是针对干净的数据库执行的。

你可以看到如何配置 database_cleaner来自 gem 的 RSpec documentation页。

关于ruby-on-rails - 我无法使用 FactoryGirl.create 创建对象,验证失败用户已被占用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com