Ruby on rails 3 模型方法在浏览器中有效,但在FactoryGirl Rails中无效
以下方法在浏览器中运行良好。它所做的一切都是将所有关联交易相加 wallet.rb 工厂.rb 钱包规格rb 考试一直不及格。我收到0,但希望15是正确的金额。同样,这在浏览器中也可以正常工作Ruby on rails 3 模型方法在浏览器中有效,但在FactoryGirl Rails中无效,ruby-on-rails-3,rspec,factory-bot,Ruby On Rails 3,Rspec,Factory Bot,以下方法在浏览器中运行良好。它所做的一切都是将所有关联交易相加 wallet.rb 工厂.rb 钱包规格rb 考试一直不及格。我收到0,但希望15是正确的金额。同样,这在浏览器中也可以正常工作 运行Rails 3.2、FactoryGirl 4.2时,FactoryGirl不将关联识别为某种功能。因此,您在上面所做的是创建一个包含属性transaction.association的事务,该属性等于:wallet 如果您只需将其声明为钱包,则您的交易将使用通过钱包工厂创建的关联钱包 不过,在定义工
运行Rails 3.2、FactoryGirl 4.2时,FactoryGirl不将
关联
识别为某种功能。因此,您在上面所做的是创建一个包含属性transaction.association
的事务,该属性等于:wallet
如果您只需将其声明为钱包
,则您的交易将使用通过钱包
工厂创建的关联钱包
不过,在定义工厂时需要小心,不要在每个方向上构建关联,因为这样很容易陷入无限循环
如果您还需要复习,请参阅FactoryGirl的文档:
至于您的问题,我建议不要依赖FactoryGirl中定义的值来进行测试。工厂的存在是为了更快地定义默认值以通过某些验证检查。不过,您不应该真正基于这些默认值进行测试。我建议进行如下测试:
it "should get the sum of the transactions" do
wallet = FactoryGirl.create(:wallet)
wallet.transactions << FactoryGirl.create(:transaction, amount: 15)
wallet.transactions << FactoryGirl.create(:transaction, amount: 10)
wallet.total_spent.should eq 25
end
它“应该得到交易的总和”做什么
wallet=FactoryGirl.create(:wallet)
我想我可能弄错了。FactoryGirl似乎确实识别了关联
,但它似乎只用于多态关联,而这里的情况并非如此。
FactoryGirl.define do
# Create a wallet
factory :wallet do
title 'My wallet'
end
# Create a single transaction
factory :transaction do
association :wallet
title 'My transaction'
amount 15
end
end
it "should get the sum of the transactions" do
transaction = FactoryGirl.create(:transaction)
wallet = transaction.wallet
wallet.total_spent.should eq 15
end
it "should get the sum of the transactions" do
wallet = FactoryGirl.create(:wallet)
wallet.transactions << FactoryGirl.create(:transaction, amount: 15)
wallet.transactions << FactoryGirl.create(:transaction, amount: 10)
wallet.total_spent.should eq 25
end