为factory_girl(数据映射器)中的has_many关联声明:child_key
我在 ror 应用程序中使用 datamapper 和 postgres,在我的模型中我有这样的关联:
#/models/account.rb
has n, :transfers_out, "Transfer", :child_key => [ :account_from_id ]
has n, :transfers_in, "Transfer", :child_key => [ :account_to_id ]
#/models/transfer.rb
belongs_to :account_from, "Account", :child_key => [:account_from_id], :required => true
belongs_to :account_to, "Account", :child_key => [:account_to_id], :required => false
现在我需要使用 Factory Girl 在 rspec 中进行测试。所以,我写了这个:
#/factories/account.rb
Factory.define :account do |f|
f.transfers_out {|transfer| [transfer.association(:transfer)]}
f.transfers_in {|transfer| [transfer.association(:transfer)]}
f.amount "0"
end
Factory.define :account_big, :class => :account do |f|
f.name "MyMillionDollarPresent"
f.amount "10000"
end
Factory.define :account_small, :class => :account do |f|
f.name "PoorHomo"
f.amount "100"
end
和小转账工厂
Factory.define :transfer do |f|
f.id "1"
f.comment "payment"
f.status "proposed"
f.amount "0"
end
所以,我尝试测试从帐户创建转账:
describe Transfer do
before(:each) do
@account_big = Factory(:account_big)
@account_small = Factory(:account_small)
@transfer = Factory(:transfer)
end
it "should debit buyer" do
@buyer = @account_big
@buyer.transfers_out = @transfer
@transfer.amount = 3000
@buyer.amount -= @transfer.amount
@buyer.amount.should == 7000
end
但这导致我测试失败:
1) Transfer should debit buyer
Failure/Error: @buyer.transfers_out = @transfer
TypeError:
can't convert Transfer into Array
# ./spec/models/transfer_spec.rb:15:in `block (2 levels) in <top (required)>'
Soo,我应该做什么以及我应该如何声明与孩子的关联在这种情况下关键?将不胜感激任何帮助。
I use datamapper and postgres for my ror application, in my models i have such associations:
#/models/account.rb
has n, :transfers_out, "Transfer", :child_key => [ :account_from_id ]
has n, :transfers_in, "Transfer", :child_key => [ :account_to_id ]
#/models/transfer.rb
belongs_to :account_from, "Account", :child_key => [:account_from_id], :required => true
belongs_to :account_to, "Account", :child_key => [:account_to_id], :required => false
Now i need to test in rspec by using factory girl. So, I've wrote this:
#/factories/account.rb
Factory.define :account do |f|
f.transfers_out {|transfer| [transfer.association(:transfer)]}
f.transfers_in {|transfer| [transfer.association(:transfer)]}
f.amount "0"
end
Factory.define :account_big, :class => :account do |f|
f.name "MyMillionDollarPresent"
f.amount "10000"
end
Factory.define :account_small, :class => :account do |f|
f.name "PoorHomo"
f.amount "100"
end
and little transfer factory
Factory.define :transfer do |f|
f.id "1"
f.comment "payment"
f.status "proposed"
f.amount "0"
end
So, I've tried to test creation of transfer from account:
describe Transfer do
before(:each) do
@account_big = Factory(:account_big)
@account_small = Factory(:account_small)
@transfer = Factory(:transfer)
end
it "should debit buyer" do
@buyer = @account_big
@buyer.transfers_out = @transfer
@transfer.amount = 3000
@buyer.amount -= @transfer.amount
@buyer.amount.should == 7000
end
But that results me with failed test:
1) Transfer should debit buyer
Failure/Error: @buyer.transfers_out = @transfer
TypeError:
can't convert Transfer into Array
# ./spec/models/transfer_spec.rb:15:in `block (2 levels) in <top (required)>'
Soo, what should i do and how should i declare the association with the child key in this situation? Would be thankful for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@buyer.transfers_out
是一个数组,@transfer
是单个对象。如果您想创建一个只有一个元素的数组,您应该使用@buyer.transfers_out = [ @transfer ]
或类似@buyer.transfers_out << @转移
。@buyer.transfers_out
is an array and@transfer
is a single object. If you want to make an array with one element you should use@buyer.transfers_out = [ @transfer ]
or something like@buyer.transfers_out << @transfer
.