Giter VIP home page Giter VIP logo

Comments (5)

paulelliott avatar paulelliott commented on August 25, 2024

To fabricate an embedded document, you need to make sure you set the parent. Let's say you have the following objects

class State
  include Mongoid::Document
  embeds_many :cities
end

class City
  include Mongoid::Document
  embedded_in :state
end

Then you would set the state when you fabricate the city. The safest thing to do is define the Fabricator like this:

Fabricator(:city) do
  state
end

I always recommend putting the minimum in the fabricator that is necessary to generate a valid object. In the case of embedded documents, the parent is required.

If you want to override that in your tests, you would fabricate it like this:

state = Fabricate(:state)
Fabricate(:city, state: state)

from fabrication.

jfrey avatar jfrey commented on August 25, 2024

Paul, do you have any thoughts on this with respect to embedded polymorphic documents?

from fabrication.

paulelliott avatar paulelliott commented on August 25, 2024

Here is the example from the mongoid documentation.

class Office
  include Mongoid::Document
  embeds_many :addresses, as: :locatable
end

class Person
  include Mongoid::Document
  embeds_many :addresses, as: :locatable
end

class Address
  include Mongoid::Document
  embedded_in :locatable, polymorphic: true
end

You will have a problem defining the parent in the embedded document's fabricator, as it could be anything. You could have multiple fabricators for the different types and use them when appropriate though.

Fabricator(:address) do
  street '123 Any Street'
  city 'Anytown'
end

Fabricator(:person_address, from: :address) do
  locatable { Fabricate(:person) }
end

Fabricator(:office_address, from: :address) do
  locatable { Fabricate(:office) }
end

The only real snag is that you can't persist an address without specifying the locatable. If one was more common than the other, I might set person as the default locatable for an address and then just override it for the office address fabricator.

from fabrication.

jfrey avatar jfrey commented on August 25, 2024

Thanks for the quick reply and the example this solved my issue persisting. Much appreciated!

from fabrication.

 avatar commented on August 25, 2024

you know I have experience with factorygirl and I think fabrication is absolutely better than that, especially for mongoid and for its speed. Thak you for your contribution

from fabrication.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.