Paperclip/Rspec tests: Is there a faster way to test paperclip validates_attachment_content_type?

1.5k Views Asked by At

One thing I've noticed is that in most of the projects I do, the one spec that always takes a long time (30 seconds +) is this shoulda/paperclip helper:

it { should validate_attachment_content_type(:bannerimage)
  .allowing('image/png', 'image/jpeg', 'image/gif', 'image/jpg')
  .rejecting('text/plain')
}

I'd quite like to keep content type validation in, but I'm wondering if there's a speedier way to do it. I already tag these tests with a :slow and run rspec without :slow specs, but nonetheless, I'm hoping someone has a swifter way of testing image content types.

1

There are 1 best solutions below

1
On

Looks like you're running your own tests against paperclip.

Generally I let gem providers (especially for large products like this one) certify that their specs will run successfully before pushing a release.

I stub out actual paperclip stuff from my tests to make them faster like this, placed in spec_helper.rb

# stub out paperclip? http://pivotallabs.com/stubbing-out-paperclip-imagemagick-in-tests/
# only like .1 seconds faster anyways though...
module Paperclip
  def self.run cmd, params = "", expected_outcodes = 0
    case cmd
    when "identify"
      return "100x100"
    when "convert"
      return
    else
      super
    end
  end
end

class Paperclip::Attachment
  def post_process
  end
end