[rspec-users] "lambda Should Change" Behavior Failing When Checking Time

Rodrigo Rosenfeld Rosas lbocseg at yahoo.com.br
Sun Nov 8 20:40:55 EST 2009

Carlos Rodriguez escreveu:
> Thanks to everyone that responded to my question.
> Here is what I ended up doing to make the spec pass:
> time_now = Time.now
> Time.stub!(:now).and_return(time_now)
> lambda { @post.publish! }.should change(@post,
> :published_at).from(nil).to(time_now)
I have thought on this approach after I realized that ActiveRecord would 
not probably populate this field using SQL since it doesn't know per 
itself about published_at attribute and I realized that you probably 
used something like "published_at = Time.now" on an after_save or 
before_save hook.

I would suggest you this approach but then, in the middle of the message 
I realized it wouldn't be a good idea to mock the Time class, since it 
wouldn't test what you want, I thought...

Unless you really care if the implementation uses Time.now to fill the 
published_at field (instead of a SQL now(), or some trigger...), the 
other alternative would be less dependent on implementation...

And looking at the complete solution, I really don't think it is clearer 
or more compact to read :)

But I guess you were intrigued on how to do that :) It happens to me 
sometimes... Today, I've spent the morning reading Rspec and Webrat 
internals just to figure out that the error I was getting was really a 
silly mistake (I was using assert_not_contain before requesting a URL).

Sometimes, things just bother us because we actually want to know how 
something works... I was really intrigued about what describe/context/it 
did from behind the scenes. And even more intrigued in trying to 
understand how webrat integration works. Every time I had a problem, I 
blessed the fact that I didn't understand the internals, but, although 
knowing the internals better helps to solve all sort of confusion, the 
problem usually is something simpler :)

But, at least, it is good when we solve all the mystery, right? :)

Good night,


Faça ligações para outros computadores com o novo Yahoo! Messenger 

More information about the rspec-users mailing list