为factory_girl(数据映射器)中的has_many关联声明:child_key

发布于 2024-11-18 06:58:40 字数 1867 浏览 10 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2024-11-25 06:58:40

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文