Friday, 15 May 2015

ruby on rails - How can I use Rspec to test that a model using Paperclip is validating the size of an uploaded file? -



ruby on rails - How can I use Rspec to test that a model using Paperclip is validating the size of an uploaded file? -

the model:

class attachment < activerecord::base belongs_to :narrative attr_accessible :description, :user_id, :narrative_id has_attached_file :file validates_presence_of :user_id validates_presence_of :narrative_id validates_attachment :file, :presence => true, :size => {:less_than => 20.megabytes} end

the test doesn't work:

describe attachment { should validate_presence_of :file } { should validate_size_of :file } # validate_size_of not exist end

i avoid dumping 20 mb file repo test this. there way similar 1 tried above work?

the best way i've done utilize built-in shoulda matchers paperclip. documentation @ link good, here's overview docs of can it:

in spec_helper.rb, you'll need require matchers:

require "paperclip/matchers"

and include module:

spec::runner.configure |config| config.include paperclip::shoulda::matchers end

example validates attachment size:

describe user { should validate_attachment_size(:avatar). less_than(2.megabytes) } end

if you're interested, source matchers can found on github

ruby-on-rails ruby rspec paperclip

No comments:

Post a Comment