0

我正在尝试制作一个具有嵌套元素并与另一个类具有 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

当我从提供者制造商中删除报价(计数: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)>'

当我从报价制造商测试中完全删除 providers(count: 3) { Fabricate(:price).workdone.provider } 关联时,当然不会创建提供者

有谁知道我如何创建提供者?

4

1 回答 1

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

此外,如果您动态生成值,则只需要使用块语法。在所有静态值的情况下,直接将它们传入(如上所示)会给您带来更少的开销。

于 2014-06-08T00:14:52.620 回答