以下方法在浏览器中运行良好。它所做的所有事情都需要所有关联的交易,并将它们的总金额加在一起。
钱包.rb
has_many :transactions
# Sums the transaction amounts together
def total_spent
transactions.map(&:amount).sum
end
工厂.rb
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
wallet_spec.rb
it "should get the sum of the transactions" do
transaction = FactoryGirl.create(:transaction)
wallet = transaction.wallet
wallet.total_spent.should eq 15
end
测试一直失败。我收到 0,但希望 15 是正确的数量。同样,这在浏览器中运行良好!
运行 Rails 3.2,FactoryGirl 4.2