ruby-on-rails-4mongoidfabrication-gem

Rails Fabricator gem with mongoid has_many_and_belongs_to relationship


I'm trying to fabricate a class that has nested elements and has HMABT relationship with another class. Here are my classes

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

These are my fabricators

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

When I fabricate quote with Fabricate(:quote)

It gives out this error message

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

When I remove quote(count: 1) from provider fabricator it gives out this error. This works on rails console by the way - providers are created.

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)>'

When I completely remove the providers(count: 3) { Fabricate(:price).workdone.provider } association from quote fabricator tests pass but of course providers are not created

Does anyone have idea how I can create providers?


Solution

  • Would it be possible to pull three providers from the generated user for this quote? You could do so with the below Fabricator definition.

    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

    Also, you only need to use the block syntax if you are dynamically generating values. In the case of all your static values, just passing them in directly (as in the above) will give you a little less overhead.