[rspec-users] mongo_mapper validations, rpsec

David Chelimsky dchelimsky at gmail.com
Wed Oct 5 09:15:09 EDT 2011


On Sep 3, 2011, at 4:17 PM, Chris Habgood wrote:

> I have a basic user class and doing rspec validations.  When I do a factory create to produce validations it blows up before I can get to the second line to check for errors.  Ideas how to get this to perform like AR Validations?
> 
> MongoMapper::DocumentNotValid:
>        Validation failed: Password can't be blank, Email can't be blank, Password digest can't be blank
> 
> class User
>   include MongoMapper::Document
>   include ActiveModel::SecurePassword
>   
>   attr_accessible :email, :password
>   
>   key :password_digest, String
>   key :password, String, :required => true
>   key :email, String, :required => true
>       
>   has_secure_password
> end
> 
> it "should not authenticate with incorrect password" do
>       user = Factory(:user, :email=> '', :password => '').should have(2).errors

Try Factory.build instead of Factory (which is an alias for Factory.create):

  user = Factory.build(:user, :email => '', :password => '')
  user.should_not be_valid # this triggers the validation
  user.should have(1).error_on(:email)
  user.should have(1).error_on(: password)

HTH,
David

>       #user.should have(2).errors #_on(:name)
>   end



More information about the rspec-users mailing list