[rspec-users] mongo_mapper validations, rpsec

Pieter Michels pieter at noort.be
Wed Oct 5 16:02:54 EDT 2011


Hi

I sort of fixed the issue. Let me first describe it some more.

My tests kept generating the following failed test:

  1) Portfolio require a valid e-mail address
     Failure/Error: port = Factory :portfolio, :email => email
     MongoMapper::DocumentNotValid:
       Validation failed: Email is invalid
     # ./spec/models/portfolio_spec.rb:44:in `block (3 levels) in <top
(required)>'
     # ./spec/models/portfolio_spec.rb:43:in `each'
     # ./spec/models/portfolio_spec.rb:43:in `block (2 levels) in <top
(required)>'

This tells me my validation is working as it should be. But although
the code in production reacts normally on 'update_attributes' or
'save' by returning a 'false', in my test the
MongoMapper::DocumentNotValid: is raised.

I fixed it by using the following test code:

lambda { port = Factory :portfolio, :email => '' }.should
raise_error(MongoMapper::DocumentNotValid)

This way I test for a working validation.

I believe I have some setting that is causing this behaviour. I also
tested it out in Rails console where it works as expected:
port = Portfolio.find_by_name('test')
port.name = 'new_name'
port.save
=> false
port.update_attributes(:name => 'new_name')
=> false


Any ideas?


regards


On Oct 5, 3:15 pm, David Chelimsky <dchelim... at gmail.com> wrote:
> 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
>
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list