私は私のモデルでは、私は、そのような関連を持っている、DataMapperのと私のRORアプリケーション用のpostgresを使用します。宣言:factory_girlでhas_manyのアソシエーションのchild_key(DataMapperの)
#/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
は、今私は、工場出荷時の女の子を使って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)>'
さて、私は何をすべきですか、この状況で子キーとの関連付けをどうすればいいですか?助けてくれてありがとう。
あなたの試したf.transfers_out Factory(:transfer)はありますか? – corroded