0

你好,我正在尝试做测试,但我有一些问题。首先,文件conn.rb

FactoryGirl.define do
    factory :conn, class: Connection do
    from_id         1
    to_id       4
    end
end

第二个文件,leg.rb

FactoryGirl.define do
factory :leg_le, class: Leg do
    association :connection_id, factory: :conn
    from_id         1
    to_id           2
end
end

我的目标是leg_le使用IDof创建conn

 conn=FactoryGirl.create(:conn)
 leg=FactoryGirl.create(:leg_le)

connection_id它似乎创造了但是nil

4

1 回答 1

0

您可以尝试:

FactoryGirl.define do
    factory :connection do
        from_id    1
        to_id      4
    end
end

FactoryGirl.define do
    factory :leg do
        association :connection, factory: :connection
        from_id         1
        to_id           2
    end
end

然后

FactoryGirl.create(:leg).connection_id
    # => 1 or whatever your connection_id is

或(如果您想在其他地方重用连接实例):

connection = FactoryGirl.create(:connection)
FactoryGirl.create(:leg, :connection=> connection).connection_id
    # => 1 or whatever your connection_id is
于 2013-09-12T16:38:48.750 回答