Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 12/28/2009: Rspec 1.2.9 and mocking flash

Ask for Help

“Ever since we upgraded to RSpec 1.2.9, we haven’t seen any stack traces. What gives?”

One of our projects lost stack traces as soon as they upgraded to RSpec 1.2.9. Reverting to RSpec 1.2.8 fixed the problem. No other projects have reported the issue yet.

Interesting Things

  • Working with Rails for several years means that, as Rails advances, our testing/mocking codes get stale. We just discovered that one of our old mocks for representing the ‘flash’ object no longer works as designed with the current version of Rails.

More on the FlashHash

Here’s an approach that used to work in Rails 2.1/2.2:

  • Stub FlashHash.new to create a MockFlashHash rather than a real
    FlashHash object.
  • MockFlashHash does nothing when sweep is called. The sweep method cleans out the flashes that have been used, and the act of rendering uses up all of the flash.now objects.
  • You can enhance MockFlashHash to behave the same way that a view would use a flash, allowing your tests to assert that a response should show a particular flash, without specifying whether you used flash or flash.now to do it.

Rails 2.3 changes the Flash cycle from what earlier versions of Rails did.

In Rails 2.2 and before, calling ‘flash’ accessed a session variable directly (assuming you have a session). You would then manipulate that session variable, which was an instance of FlashHash. It was therefore fairly easy to replace FlashHash with another type of object.

In Rails 2.3, the cycle is a little bit different. Instead of manipulating the session directly, calling ‘flash’ sets an instance variable. At the end of the request, that session variable is stored to the session using a new method signature – flash.store(session). (Note that flash extends Hash, so this overrides the standard Hash.store)

This approach breaks encapsulation, forcing the FlashHash object to know about the session and forcing any mock replacements to know how to store themselves to the session.

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *