Using fixtures with fabricators

46 views
Skip to first unread message

Paul Sadauskas

unread,
Sep 5, 2014, 5:37:20 PM9/5/14
to fabrica...@googlegroups.com
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.

```ruby
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:

```ruby
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:

```ruby
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

Paul Elliott

unread,
Sep 5, 2014, 6:17:48 PM9/5/14
to fabrica...@googlegroups.com
Hey Paul,

I think integrating them is a pretty cool idea. Do you think you'd be able to put together a pull request with some failing specs that show how you'd like it to work. I would be happy to implement it if you could do that. I created an issue on GitHub (https://github.com/paulelliott/fabrication/issues/220) to track this further so let's continue the conversation there.


-- Paul


--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages