Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Paperclip file uploading with RSpec

I don't really care about testing file uploads, but since I have validates_attachment_presence, etc.. in my model, rspec is complaining.

So now I'm creating my model with these attributes in the spec to try and shut it up:

@attr = {
  :name => "value for name",
  :title => "value for title",
  :content => "value for content",
  :pic_file_name => "example.jpg",
  :pic_content_type => "image/jpg",
  :pic_file_size => "8192",
  :pic_updated_at => nil
}

This doesn't work, though.

I found this: http://fr.ivolo.us/posts/mocking-paperclip-with-rspec So I tried something like this:

Post.should_receive(:save_attached_files).and_return(true)

Which doesn't work either. How do I appease RSpec?

like image 245
nnyby Avatar asked Jul 28 '10 23:07

nnyby


People also ask

How do I test an RSpec file?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

Is RSpec used for unit testing?

RSpec is a unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool.

What type of testing is RSpec?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

How do you use a paperclip gem in rails?

Setting Up PaperclipTo set up Paperclip, first we need to install the ImageMagick dependency. Paperclip uses ImageMagick to resize images after upload. If you are using another system, you can get download and install instructions from the ImageMagick website. Run bundle install to finish it up.


1 Answers

If the model has_attached_file :pic, you should be able to just point the pic attribute at some file and all should be dandy.

Meaning something like @attr = { :pic => File.open(File.join(Rails.root, 'spec', 'fixtures', 'file.png')) }

like image 82
theIV Avatar answered Sep 26 '22 13:09

theIV