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

1

13

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.

Resorcinol answered 16/11, 2011 at 4:50 Comment(4)
This spec should run fast since this matcher does not perform any real image upload/writing stuff, check github.com/thoughtbot/paperclip/blob/master/lib/paperclip/… I think the problem might be buried somewhere little deeper. Could you paste the whole spec or log/test.log output?Helms
Strangely enough - it does. These days, these sortsa tests typically run in < 0.5s. ...I wish I could help out, but the reference to wherever I was having this issue is long since forgottenResorcinol
I was the one who started the bounty. Using Paperclip 2.x is still very slow. Is this solved in 3.x?Inga
My memories of solving this are a little hazy, but I think 3.0 upgrade may have done the trick. Certainly my 3.0 projects don't have this issue.Resorcinol
V
0

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
Visser answered 6/8, 2013 at 20:22 Comment(1)
That's because you're not stubbing it out correctly. As of today, the correct way to stub it would be: gist.github.com/gabrielecirulli/6340651 (it might change in the future due to changes in the code structure of paperclip)Nette

© 2022 - 2024 — McMap. All rights reserved.