ruby on rails - Factory Girl with implicit has_many association -



ruby on rails - Factory Girl with implicit has_many association -

i'm trying test model has implicit has_many association, , having difficulties.

i have table a, column b_id, b foreign key - except there no table b in database, or active record class associated object b. there table c has column b_id.

in model table c have:

# implicit has_many :alphas def alphas alpha.where(:b_id => b_id).order(:xyz) end

this database construction makes sense info have, , non-test code works fine.

my test code works, , hope missing simple.

i have factories defined , c, , have test:

a1 = factory(:alpha, :b_id => 123, :xyz => 100) a2 = factory(:alpha, :b_id => 123, :xyz => 200) c1 = factory(:c, :b_id => 123) puts c1.alphas.count puts c1.alphas.first c1.alphas.first.should == a1

the output is:

2 nil <test fails>

changing number of objects share b_id result in c1.alphas.count changing, can't seem within implicit association , object - instead nil. there other methods in c model can't test because methods need access fields on individual objects.

does have insight going behind scenes here, or might around this? thanks.

take @ illustration have admin_user role of admin.

https://github.com/drhenner/ror_ecommerce/blob/master/spec/factories/user.rb

in case roles not factory.

i find best next though.

@order = factory(:order) order_item = factory(:order_item, :total => 5.52 ) @order.stubs(:order_items).returns([order_item, order_item])

or

@order = factory(:order) order_item = factory(:order_item, :total => 5.52, :order => @order )

ruby-on-rails rspec factory-girl

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -