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

Zubin Henner lists at ruby-forum.com
Tue Apr 15 19:01:31 EDT 2008

Ashley Moran wrote:
> Any reason why you are not using pure mocks here?  I assume it's not
> Item you are testing, but some other model that is associated with it?

Hi Ashley, yes, you're spot on - I'm using other associated models. I 
just simplified it for illustration. Here's what I'm really testing:

# order.rb
has_one :freight_selection
def calculate_freight_cost(freight_method)
  # ... (based on order total and freight method)

# freight_selection.rb
belongs_to :order
belongs_to :freight_method
before_validation :set_freight_price

def set_freight_price
  self.price = order.calculate_freight_cost(freight_method)

def validate
  # ... (confirms that freight_method is valid for order's shipping 

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?
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list