Strange validation errors

Hey guys,

I’m having a serious rspec-problem at the moment:

I keep getting strange, empty validation errors when I run the whole
spec-suite:

#################################################################
1)
ActiveRecord::RecordInvalid in ‘LikesController DELETE destroy’ should
change like-count’
Validation failed:
my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/
active_record/validations.rb:1090:in save_without_dirty!' my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/ active_record/dirty.rb:87:insave_without_transactions!’
my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/
active_record/transactions.rb:200:in save!' my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/ active_record/connection_adapters/abstract/database_statements.rb: 136:intransaction’
my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/
active_record/transactions.rb:182:in transaction' my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/ active_record/transactions.rb:200:insave!’
my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/
active_record/transactions.rb:208:in rollback_active_record_state!' my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/ active_record/transactions.rb:200:insave!’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
factory_girl/proxy/create.rb:6:in result' my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/ factory_girl/factory.rb:316:inrun’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
factory_girl/factory.rb:260:in create' my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/ factory_girl/proxy/build.rb:17:inassociate’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
factory_girl/attribute/association.rb:15:in add_to' my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/ factory_girl/factory.rb:313:inrun’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
factory_girl/factory.rb:311:in each' my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/ factory_girl/factory.rb:311:inrun’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
factory_girl/factory.rb:260:in create' my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/ factory_girl/factory.rb:291:insend’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
factory_girl/factory.rb:291:in default_strategy' my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/ factory_girl.rb:21:inFactory’
./spec/controllers/likes_controller_spec.rb:46:
#################################################################

Here is the corresponding spec:

#################################################################
it “should change like-count” do
we_are_logged_in_as(get_member_user)
entry = Factory(:entry)
Factory(:like, :user => get_member_user, :entry => entry)
lambda do
post ‘destroy’, :entry_id => entry.id
end.should change(Like, :count).by(-1)
response.should be_success
end
#################################################################

Trouble-Shooting turns out to be difficult:

The spec itself seems to be correct, when running the spec-file alone
I don’t get any errors.
I only get this error when running the whole spec-suite - running the
single spec file is fine:

#################################################################
spec spec/controllers/likes_controller_spec.rb
Example disabled: should change like-count on success

Finished in 0.756284 seconds

3 examples, 0 failures
#################################################################

I have a lot of other specs failing in the very same way.
The errors itself are confusing - empty validation error messages are
utterly meaningless.

I suspect that there is something going wrong with all the stubbing /
mocking within in my specs.

To be concrete:

I suspect that something like:

Foo.any_instance.stubs(:bar).returns…

within a spec isn’t properly isolated from the other specs, thus
causing strange validation errors.

What do you guys think?
How could I narrow down this error further?

Sys-Info:
OS: Ubuntu 9.10
spec -v
rspec 1.3.0
Mocking / Stubbing: Mocha 0.9.8

On Mon, Mar 15, 2010 at 6:54 AM, jollyroger
[email protected] wrote:

change like-count’
my_project/vendor/bundled_gems/gems/activerecord-2.3.5/lib/
factory_girl/factory.rb:316:in `run’
my_project/vendor/bundled_gems/gems/factory_girl-1.2.3/lib/
#################################################################
end.should change(Like, :count).by(-1)

I have a lot of other specs failing in the very same way.
Foo.any_instance.stubs(:bar).returns…
rspec 1.3.0
Mocking / Stubbing: Mocha 0.9.8

Take a look at this thread and see if it helps:

http://groups.google.com/group/mocha-developer/browse_thread/thread/5c2b8b28f700bb17?hl=en

Hey David,

I experienced similar problems as depicted above in other projects now
as well.
E.g. a wrong-used stub in an it-block overwrote stubs established in
helper functions for the whole spec-file etc.
This was a mocha-related problem, I know that for sure cause when I
swapped out mocha for rspec-stubbing everything behaved as expected.
I now switched from mocha to rspec-stubbing / mocking, and so far,
works like a charm - thanks for your time and help.