views:

372

answers:

2

Hi!

I'd like to spec the fact that my application layout view prints out flash notices. However the following code does not run, the flash method does not exist in view specs (as opposed to controller specs where it works perfectly):

describe 'layouts/application' do
  it "renders flash notices" do
    flash[:notice] = "This is a notice!"
    render
    response.should contain "This is a notice!"
  end
end

Is my code wrong or is it a "not-yet-implemented feature" in Rspec 2? I'm on Rails3 and Rspec2 from its master branch on Git.

Thanks!

A: 

I don't think there's an easy way to do this with Rspec. However, it's a perfect application for Cucumber, where you could write something like this:

  Scenario: Layout displays flash messages
    Given a layout
    When I render the view
    Then I should see "This is a notice!"

Of course your scenario would do something unique to your application, so you could test that the actual message you expected is being rendered.

zetetic
Thank you for the hint but I'm already using cucumber, from a "feature point of view". Actually I'm developing that application in a BDD-ish way: I write a feature, run it, write specs, run them, make it pass, make the feature pass, refactor, and start over with the next feature.The cucumber scenario checks for a specific notice on the page, after some event occured in a particular context, as is expected by the end-user. The view spec focuses on the responsibilities of the application layout view itself. One of those is to display any flash notice. That's what I'm trying to spec here.
jbpros
At the risk of sounding glib, I'm going to suggest that if your feature sees that the flash notice was added to the response, then your job is done. The user won't care whether the notice comes from a view template, a layout, a partial, or whatever.
zetetic
That's right the user won't care. That's why in my feature there is no mention of a view or such sort of concept. But from the developer point of view I want to ensure that my view does its job properly and that I can rely on its behavior in any context. Anyway the point is that I think there is something missing in Rspec2: flash mocking within views (and I think it was working in Rspec1).
jbpros
A: 

Funny, I was just searching here on the flash issue because my authentication cucumber stories in Rails 3 beta 3 and RSpec 2 appear to be broken where they are hitting the flash notices.

So, I think you're right. There is either some new trick in using flash notices in Rails3 with RSpec2 or RSpec2 has not implemented this.

Very problematic if you're trying to port Cucumber stories over to Rails3 right now.

Daryl
I think this issue is now resolved on master branch. I didn't test it yet but it should be okay: http://github.com/rspec/rspec-rails/issues/closed#issue/45
jbpros