jnunemaker/mongomapper — Issue #326: RSpec failing tests
Author: pierot
Created: 2011-10-13 09:52:52+00:00
State: open

My tests kept generating the following failed test:

``` Ruby
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 '
# ./spec/models/portfolio_spec.rb:43:in `each'
# ./spec/models/portfolio_spec.rb:43:in `block (2 levels) in '
```
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:

``` Ruby
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:

``` Ruby
port = Portfolio.find_by_name('test')
port.name = 'new_name'
port.save
=> false
port.update_attributes(:name => 'new_name')
=> false
```

Is this a bug? Any other ideas?