Rails Fabricator gem与mongoid has_many_and_belongs_to的关系

时间:2014-06-06 17:42:14

标签: ruby-on-rails-4 mongoid fabrication-gem

我正在尝试构建一个具有嵌套元素并且与另一个类具有HMABT关系的类。这是我的课程

class Quote

  has_and_belongs_to_many :providers
  belongs_to :user 

class Provider

  has_and_belongs_to_many :quotes 
  belongs_to :user
  embeds_many :workdones


class Workdone
  embedded_in :provider  
  embeds_many :prices

class Price
  embedded_in :workdone

这些是我的制作者

Fabricator(:price) do 
  workdone
  variation_id {Fabricate(:variation)._id}
  price {12}
  discount {5} 
end 

usern = Faker::Name.last_name
uidn = Faker::Number.number(10) 
Fabricator(:user) do
    uid 123456
    username {usern }
    email {Faker::Internet.email}
    username_or_uid { [ usern , uidn] }
    provider {'facebook'}
    name {Faker::Name.name }
    gender {'male'}
    birthday { Time.at(rand * (18.years.ago.to_f - 50.years.ago.to_f) + 50.years.ago.to_f).to_date } 
end

Fabricator(:workdone) do 
    provider 
    workdonecount {1}
    quotegivencount {1}
    rating {5}
    active {true}
    give_price {true}
end


Fabricator(:provider) do
  user 
  business_address {"Bostanlı Mh., 6352. Sokak No:15, 35480 İzmir, Türkiye"}
  business_description {"Biz de sadece epilasyon işleriyle uğraşıyoruz ve bu işlerin  
  quote(count: 1)

 Fabricator(:quote) do 
    user  
    providers(count: 3) { Fabricate(:price).workdone.provider } 
    share_on_facebook_timeline {false} 
    customer_address {"Bostanlı Mh., 6352. Sokak No:15, 35480 İzmir, Türkiye"}
    description {"dasdsadasdsad sadadsssssdsadsasdas"}
    location {[27.094637499999976,38.4621336 ] }
    variation_id { Fabricate(:variation)._id}
end

当我使用Fabricate(:quote)

制作报价时

它会发出此错误消息

   Quote#give_quote works
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep

当我从提供者制造商中删除引用(count:1)时,它会发出此错误。 顺便说一下,这适用于rails控制台 - 创建提供程序。

Failure/Error: quote = Fabricate(:quote)
     TypeError:
       no implicit conversion of nil into String
     # ./spec/fabricators/quote_fabricator.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/quote_spec.rb:51:in `block (3 levels) in <top (required)>'

当我完全删除提供者时(数:3){Fabricate(:price).workdone.provider} 引用制造商测试的关联通过但当然没有创建提供者

有没有人知道如何创建提供者?

1 个答案:

答案 0 :(得分:0)

是否可以从生成的用户中提取三个提供商以获得此报价?您可以使用以下Fabricator定义执行此操作。

Fabricator(:quote) do user providers { |attrs| attrs[:user].providers.sample(3) } share_on_facebook_timeline false customer_address "Bostanlı Mh., 6352. Sokak No:15, 35480 İzmir, Türkiye" description "dasdsadasdsad sadadsssssdsadsasdas" location [27.094637499999976,38.4621336] variation_id { Fabricate(:variation)._id } end

此外,如果要动态生成值,则只需使用块语法。在所有静态值的情况下,只是直接传递它们(如上所述)将减少开销。