[rspec-users] before_validation callbacks bypassed when stubbing :vali

Ashley Moran ashley.moran at patchspace.co.uk
Wed Apr 16 04:30:03 EDT 2008

On 16 Apr 2008, at 00:01, Zubin Henner wrote:
> I don't need to validate the freight selection since that's handled
> elsewhere - I just want it to be valid. But I expected the
> before_validation filter to run. I've found a workaround, but I guess
> I'd like to understand this behaviour more, cause it had me stuck  
> for a
> while. Is Mocha causing this or Rails?

It's simply because the :valid? method is what triggers the filter,  
and you are overriding it.  (There's nothing in the runtime sitting  
and watching for you to *send* the message, it's handled when it's  

I suggest you take one of two approaches:

* If you are trying to build a suite of database-backed specs to prove  
the model layer works, then actually create a valid FreightSelection -  
whatever that entails.  (Actually, better to use a story here, I think.

* If you are trying to spec the Order class, then use a pure Mocha  
mock that stubs :id, :valid?, and whatever else ActiveRecord needs to  
keep it happy (look at rails_example_group.rb in the RSpec on Rails  
plugin for inspiration).  This way you have full control over the  
second object.

Hope this helps



More information about the rspec-users mailing list