4
votes

For some application I am using Paperclip for file upload (actually the dm-paperclip flavour), and Factory Girl, Rspec, Capybara for testing. I have a very simple Factory for the "Picture" model, where I am stubbing my file properties as suggested in this post:

FactoryGirl.define do
  factory :picture do
    title "My Picasso"
    description "It's like looking in a mirror."
    picture_file_file_name { 'spec/resources/img_1.jpg' }
    picture_file_content_type { 'image/jpg' }
    picture_file_file_size { 1024 }
  end
end

In diverse feature tests with Capybara, I visit pages in which the templates feature thumbnails of the Picture instances:

feature "List of Pictures", :js => true  do
  scenario "displays appropriately the index page of the pictures with pagination" do
    FactoryGirl.create_list(:picture, 21)
    visit '/pictures'
    # And more testing...
  end
end

An example of partial used in one of the templates:

=  content_tag_for(:li, picture, :class => 'listed_picture') do
  = link_to picture_path(picture) do
    - if picture.picture_file?
      = image_tag picture.picture_file.url(:thumb)

The problem I have now, is whenever I run the specs, the test fails because there is no matching route for the thumbnail url:

No route matches [GET] "/system/picture_files/1/thumb/img_1.jpg"

Is there any way to stub Paperclip's helper methods to make the test pass?

Thanks in advance for any help!

1
There are some matchers for Paperclip on rubydoc, i guess it should help. - zishe
Thanks for the reply zishe. However I'm not really looking for matchers to test validation on my model. My aim is to mock/stub a valid path for my image_tag helpers relative to the model instances stubbed by the factory when the templates are rendered with Capybara/Poltergeist/PhantomJS. Thanks again! - Concordia Discors
I'm currently trying to work through this as well. Did you make any progress? - steel
Actually I didn't @steel ... - Concordia Discors

1 Answers

1
votes

I just went through this process. Here's how I solved the issue.

First, I created a single method on the object to reference the image URL, both to abide by the law of Demeter and to make for an easier test. For you, that might look like:

#picture.rb

class Picture
...
  def picture_file_url(size = nil)
    picture_file.url(size)
  end
...
end

Now we're ready to stub the Paperclip attachment URL in the spec:

describe "List of Pictures", :js => true  do
  it "displays appropriately the index page of the pictures with pagination" do
    let(:picture) { create(:picture) }
    allow(Picture).to receive(:picture_file_url) { "url" }
    visit '/pictures'
    # And more testing...
  end
end

Hope this helps you or someone.