Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

"should validate_attachment_content_type" not working (rspec) #1158

Closed
renevall opened this issue Feb 8, 2013 · 8 comments
Closed

"should validate_attachment_content_type" not working (rspec) #1158

renevall opened this issue Feb 8, 2013 · 8 comments

Comments

@renevall
Copy link

renevall commented Feb 8, 2013

I'm doing the basic unit tests for the attachment I have for my model, but I keep getting this error

"Failure/Error: rejecting('text/plain', 'text/xml') }
NoMethodError:
undefined method `any?' for nil:NilClass "
"

Here is my model

 #thumbs
  attr_accessible :image
  has_attached_file :image, :styles => { :large => "600x550>", 
    :medium => "300x275>" }

  validates_attachment :image,
  :content_type => { :content_type => ["image/jpeg", "image/png"] },
  :size => { :in => 0..2.megabytes }

Here is the spec

describe "paperclip image" do
    it { should have_attached_file(:image) }
    it { should validate_attachment_content_type(:image).
                allowing('image/png', 'image/gif', 'image/jpeg').
                rejecting('text/plain', 'text/xml') }
    it { should validate_attachment_size(:image).
                less_than(2.megabytes) }
  end

I'm not missing "subject", actually this is the only test failing.

@renevall
Copy link
Author

renevall commented Feb 8, 2013

Upon further tests and reading code from another repo https://github.com/ffbit/simple-forum/blob/master/spec/models/user_spec.rb I tested what they had:

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

And now it seems to work, but this is different from the doc http://rubydoc.info/gems/paperclip/Paperclip/Shoulda/Matchers

@djcp
Copy link
Contributor

djcp commented Feb 8, 2013

@sikachu pointed out that the chained allowing() method calls aren't going to work properly because we don't merge them together. Beyond that, can you fork the paperclip_demo repo and replicate your problem there? It'd be helpful to see this failure in a working application.

@renevall
Copy link
Author

renevall commented Feb 8, 2013

Sure, will do. Let's see what happens

@renevall
Copy link
Author

renevall commented Feb 8, 2013

Ok so I did what @djcp told me and I could not replicate the error. I was confused on why... so I went back to my code and it was working fine even when I rolled back to my earlier version. Tried whipping the gems to discard the new gems in the paperclip demo making my code work. Nope that wasn't it.

Long story short, I realize my error and what could help someone avoid this.

My model validation was missing the 'image/gif' I was asking for in my spec

validates_attachment :image,
  :content_type => { :content_type => ["image/jpeg", "image/png"] },
  :size => { :in => 0..2.megabytes }

in my spec

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

The thing is, the error the rspec throws is so not helpful, maybe to a more experienced ruby programmer it was. Is there anything you guys could do about it?

Failure/Error: rejecting('text/plain', 'text/xml') }
NoMethodError:
undefined method `any?' for nil:NilClass

Thanks anyway, I feel a bit blind now, but I'm glad I now understand what happened

@djcp
Copy link
Contributor

djcp commented Feb 8, 2013

Glad you sorted it out, and thanks for reporting back.

@djcp djcp closed this as completed Feb 8, 2013
@aag1091
Copy link

aag1091 commented Jul 22, 2013

thanks man @ResidentBio helped to deduce my error.

@blakewest
Copy link

yeah, I ran into this same error. I fixed it, but it appears this happens whenever anything in the "allowing" method is spelled wrong. In my case, I accidentally spelled it "jpg" rather than "jpeg", and all I got was the "any?" error, which is really not helpful. If you misspell it in the model, you get a "content type is illegal", which is helpful. Anyway, thanks @ResidentBio for showing this, it led me down the right path.

@donmb1
Copy link

donmb1 commented Aug 21, 2015

I ran into the same issue today and cannot figure out how to fix it.
In my model:

validates_attachment_content_type :file, :content_type => ["application/pdf"]

In my spec file:

it { should validate_attachment_content_type(:file).allowing('application/pdf').rejecting('text/plain', 'text/xml') }

throws:

undefined method `validate_attachment_content_type' for #RSpec::ExampleGroups::EventInformation:0x007fd75b1eeee0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants