Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Using fixtures with fabricators #220

Closed
paulelliott opened this issue Sep 5, 2014 · 2 comments
Closed

Using fixtures with fabricators #220

paulelliott opened this issue Sep 5, 2014 · 2 comments
Labels

Comments

@paulelliott
Copy link
Owner

This is from a thread on the google group: https://groups.google.com/forum/#!topic/fabricationgem/9dJznFRCG74

Hello,

After reading the recent blog post at http://collectiveidea.com/blog/archives/2014/08/06/time-to-bring-back-fixtures/, I was intrigued by some of the ideas. For example, nearly every single one of my fabricators references some account or user or other "owner" object, which results in lots of extra records created and time wasted.

Fabricator :widget do
  account
  name { ... }
end

I also often times want to create several "sibling" objects with the same account, which requires a bit more boilerplate:

let(:account) { Fabricate(:account) }
let(:widget_a) { Fabricate(:widget, account: account) }
let(:widget_b) { Fabricate(:widget, account: account) }

I think by having that initial account defined as a fixture would solve both problems, first by only ever creating the one account, and having all fabricators default to using it:

Fabricator :widget do
  account { accounts(:primary_account) }
  name { ... }
end

I spent a little bit of time trying to hack this together, but failed. The issue was making the Rails #accounts fixture helper available in the fabrication Runner. But before I spent too much time on this, I wanted to get your opinion on if this was even a good idea, and if so, any feedback on how best to accomplish this.

I was hoping it would be something as simple as Fabrication::Schematic::Runner.send(:include, ActiveRecord::TestFixtures), but no dice. Any tips?

Thanks,
Paul

@superacidjax
Copy link
Contributor

This is a great idea. The Account use case is extremely common. It would be important to have an override as well, (for example a use case where you're testing 2 different accounts. "User views only their account information" -- etc.

What was the failure you got on the Fabrication::Schematic::Runner.send(:include, ActiveRecord::TestFixtures)?

@paulelliott
Copy link
Owner Author

I don't particularly want to include all of Rails, which seems to be what I need to do to make it work in the test suite. The bigger problem was in the implementation itself though. What essentially needs to happen is we add a method to Fabrication::Schematic::Evaluator that provides access to the fixtures. It doesn't seem to be as straightforward as I was hoping.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants