thoughtbot / shoulda

Makes tests easy on the fingers and the eyes
http://www.thoughtbot.com/community
MIT License
2.21k stars 184 forks source link

validates_presence_of :media (carrierwave) failing rails 4.0.4, rspec #244

Closed ghost closed 9 years ago

ghost commented 10 years ago

After updating gems shoulda validates_presence_of is kicking out an error I can't figure out.

OS X 10.7.5 ruby -v ===> ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin11.0] rails -v ===> Rails 4.0.4

bundle | grep rspec Using rspec-core 2.13.1 Using rspec-expectations 2.13.0 Using rspec-mocks 2.13.1 Using rspec 2.13.0 Using guard-rspec 2.5.0 Using rspec-rails 2.13.1

bundle | grep shoulda Using shoulda-context 1.2.1 Using shoulda-matchers 2.6.0 Using shoulda 3.5.0

bundle | grep carrier Using carrierwave 0.10.0

model

  ...
  validates_presence_of :media, message: "You must specify an mp4 file to upload"
  ...

spec file

require 'spec_helper'
...
   it { should validate_presence_of(:media).with_message("You must specify an mp4 file to upload") }
...

rspec spec/models/video_spec.rb

  1) Video Validations 
     Failure/Error: it { should validate_presence_of(:media).with_message("You must specify an mp4 file to upload") }
     Shoulda::Matchers::ActiveModel::CouldNotClearAttribute:
       Expected value to be nil, but was <VideoUploader:0x007fc159dcbb68 @model= Video id: nil, title: nil, description: nil, enabled: true, course_id: nil, sortorder: 0, created_at: nil, updated_at: nil, media: nil, survey: nil, pub_date: nil, author: nil, @mounted_as=:media>
     ./spec/models/video_spec.rb:43:in block (3 levels) in top (required)

non shoulda validation works

    it "Should not allow Video with no media file" do
      bad_vid=FactoryGirl.build(:video,media: nil)
      expect(bad_vid).to be_invalid
    end

Above test results in green 'Should not allow video with no media file"

dougfarre commented 10 years ago

Its not just during testing. Calling a validates_presence_of a carrierwave attribute will fails because the attribute won't be set until the model has been saved. Read the following:

https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-validate-uploads-with-active-record

mcmire commented 9 years ago

Closing this due to lack of activity.