ruby on rails - Why is this RSpec test failing? -


i'm in process of learning ruby on rails, treat me total neophyte, because am.

i've got user model associated rspec tests, , following test fails:

require 'spec_helper' describe user      'should require password'         user.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''}).should_not be_valid     end  end 

the relevant part of user model looks this:

class user < activerecord::base     ...     validates :password, :presence => true,                          :confirmation => true,                          :length => { :minimum => 6 }     ... end 

here's catch: if run user.new(...).valid? rails console using arguments above, returns false expected , shows correct errors (password blank).

i using spork/autotest , restarted both no avail, test fails running directly rspec. doing wrong here?

edit

i tried few more things test. fails:

        u = user.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''})         u.should_not be_valid 

so this:

        u = user.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''})         u.valid?         u.errors.should_not be_empty 

this passes, confirming :password indeed blank:

        u = user.new({:email => 'valid_email@example.com', :password => '', :password_confirmation => ''})         u.password.should == '' 

so, it's spork causing problem. can turn caching off, won't need restarting every time :

http://ablogaboutcode.com/2011/05/09/spork-testing-tip-caching-classes

i think happens :

ruby-1.9.2-p180 :020 > u = user.new  => #<user id: nil, email: ... ruby-1.9.2-p180 :021 > u.errors  => {}  ruby-1.9.2-p180 :022 > u.save  => false  ruby-1.9.2-p180 :023 > u.errors  => {:email=>["can't blank", "can't blank"], ...}  

in short, if change new create, work :) think happens because matcher be_valid checks on model validation errors. there can deeper explanation, think if use create instead of new, work.

edit : have be_valid_verbose version might help. create 'be_valid_verbose.rb' file in rspec/custom_matchers folder, , inside write :

rspec::matchers.define :be_valid_verbose   match |model|     model.valid?   end    failure_message_for_should |model|     "#{model.class} expected valid had errors:n #{model.errors.full_messages.join("n ")}"   end    failure_message_for_should_not |model|     "#{model.class} expected have errors, did not"   end    description     "be valid"   end end 

now check against be_valid_verbose instead of be_valid. present more information on happening in case.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -