0

I'm writing my User model spec with RSpec and I want to test if photo uploading works as it should - pretty straight forward.

But I haven't gotten very far. An excerpt of my model spec is like this:

it "does not allow uploading files other than images for avatar" do
  user = create(:user)
  user.avatar = File.new("#{Rails.root}/public/robots.txt")
end

I have created my user, trying to attach my file… but as to how to actually test the upload or just to validate if it indeed is an image or not (I suppose uploading isn't necessary per se, I just need to validate the attached file). No idea.

I have looked into Paperclip shoulda matchers, like for example

it { should validate_attachment_content_type(:icon).
            allowing('image/png', 'image/gif').
            rejecting('text/plain', 'text/xml') }

but to me it seems entirely pointless, as all it does is say it should validate content type. But does it? How do I know if it does? How do I attach a file and see if it does?

I have searched far and wide, with all the possible keyword combinations to find an answer to this, but with no luck so far. Am I just somehow conceptually not getting it? I'm clearly a beginner, so please don't burn me for asking what might seem dumb to you.

Ragnar
  • 520
  • 1
  • 10
  • 16
  • So according to [Paperclip-Shoulda-Matchers documentation](http://www.rubydoc.info/github/thoughtbot/paperclip/Paperclip/Shoulda/Matchers) you can test that your model has a attached file, validate presence, content type or size. Can you be more precise on your need here ? – Florent Ferry May 06 '15 at 16:44
  • Yes, that is all fine and dandy. But how do I attach said file to test against? – Ragnar May 06 '15 at 16:50
  • Like, for example, if I want to test the validity of my user, I first create my user `user = create(:user, username: 'testuser', email: 'testuser@email.com', password: 'testpassword')` and then `expect(user).to be_valid`. How do I do the same with my attachment? – Ragnar May 06 '15 at 16:54
  • You can write `let(:user) { create(:user) }`, `subject { user }` and `it { should have_attached_file(:avatar) }`. – Florent Ferry May 06 '15 at 16:57
  • That doesn't really answer my question but I appreciate your help! :) – Ragnar May 06 '15 at 17:09
  • I guess I understand your need here. In model spec, you test your model, but for testing uploading you need to do it in Controller Spec, or Request Spec, or Feature Spec, so I think this [http://stackoverflow.com/questions/7260394/test-a-file-upload-using-rspec-rails](http://stackoverflow.com/questions/7260394/test-a-file-upload-using-rspec-rails) should answer your question. – Florent Ferry May 06 '15 at 17:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77112/discussion-between-florent-ferry-and-ragnar). – Florent Ferry May 06 '15 at 17:17

1 Answers1

0

You need to look at the source for Paperclip's validate_attachment_content_type matcher to make sure that you are happy with how they are testing things.

See: https://github.com/thoughtbot/paperclip/blob/dca87ec5d8038b2d436a75ad6119c8eb67b73e70/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46