相关疑难解决方法(0)

在factory_girl中填充与子项的关联

我有一个模型Foohas_many'Bar'.我为每个对象都有一个factory_girl工厂.Bar的工厂与Foo有关联; 它会在创建Bar时实例化Foo.

我想要一个创建包含Bar的Foo的Factory.理想情况下,这个Bar将通过:bar工厂创建,并尊重用于创建Foo的构建策略(create/build).

我知道我可以调用:bar工厂,然后从新的Bar中获取Foo引用.我想避免这种情况; 在我的测试用例中,重要的对象是Foo; 称酒吧工厂似乎有点迂回曲折.此外,我可以看到需要一个有多个条形的Foo.

这在Factory_girl有可能吗?你如何在父母中定义这种关系?

ruby unit-testing factory ruby-on-rails factory-bot

42
推荐指数
1
解决办法
2万
查看次数

Rails 4.2.6具有多态关联的FactoryGirl

我在为FactoryGirl的多态关联设置工厂时遇到问题.我的模型和工厂设置如下所示:

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
end

class Customer < ActiveRecord::Base
  has_one :address, as: :addressable
end 

factory :address do
      village Faker::Address.city
      upazilla Faker::Address.street_name
      ward Faker::Address.street_address
      district Faker::Address.state
      association :addressable
end


factory :customer ,class: Customer do 
      recharge_token 10 
      date_of_birth Faker::Date.backward(100)
      manager
      after(:create) do |customer| 
         customer.address = create(:address, addressable: customer)
        #create( :address, addressable: customer)
      end     
end
Run Code Online (Sandbox Code Playgroud)

测试套件打破以下错误消息

 vendor/cache/ruby/2.2.0/gems/factory_girl-4.7.0/lib/factory_girl/linter.rb:12:in `lint!': The following factories are invalid: (FactoryGirl::InvalidFactoryError)
 * address - Factory not registered: addressable (ArgumentError)
Run Code Online (Sandbox Code Playgroud)

正是我的问题,但不幸的是他的解决方案对我不起作用.谢谢大家的时间!

ruby-on-rails rspec-rails ruby-on-rails-4 factory-bot

2
推荐指数
1
解决办法
2097
查看次数