Fabricator for an image object.

20 views
Skip to first unread message

Diego DeSouza

unread,
Jan 29, 2016, 12:01:45 PM1/29/16
to fabrication
I am trying to create an object that has an attribute image
I am wondering if there's a way to Fabricate it.

I have tried this.

Fabricator(:image) do
  filename {"fake_image.png"}
end

then in my object

Fabricator(:project) do
  title { sequence(:title) {|i| "This cool title number #{i}"} }
  event_date Date.today
  featured_image { Fabricate(:image) }
  text "Stuff"
end

Still doesn't go through the validation. 

Suggestions?

Paul Elliott

unread,
Jan 29, 2016, 12:09:13 PM1/29/16
to fabrica...@googlegroups.com
Fabrication is setting a field on your `Image` model called `filename` to the string value provided ("fake_image.png").

To get the behavior you want, it may depend on what is handling your image attachments. If you were using carrierwave your model would look something like this:

```
class Project
  mount_uploader :featured_image
end
```

That will create a field on your model called `featured_image` and allow you to set an actual file object on it. Let's say you have an image file for your test suite at `spec/support/fake_image.png`. Your fabricator would look like this:

```
Fabricator(:project) do
  featured_image { File.open(File.join(Rails.root, 'spec', 'support', 'fake_image.png')) }
end
```

It's actually providing the the image file to the field, not just the name of the file.


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