Forum: RSpec Driving Out A View - Layouts and Sessions?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lee (Guest)
on 2009-06-05 19:42
(Received via mailing list)
I am driving out a view and I want to test that a link is displayed.

The link_to code for the link is included in the layout for the view
rather than the view template itself.

Here's my example:

  it "should display a link to create a new zone" do
    render "contexts/index.html.erb", :layout => "layouts/
contexts.html.erb"
    response.should contain("sign out")
  end

Firstly, my experiments suggest that the layout is only rendered if
the :layout attribute is included in the call to render. Is my
understanding correct?

Secondly, the link_to code only executes if the session contains a
piece of data (member id). How do I set up the session data in my view
spec?

Thanks.
Stephen E. (Guest)
on 2009-06-05 20:52
(Received via mailing list)
On Fri, Jun 5, 2009 at 11:40 AM, Lee<removed_email_address@domain.invalid> 
wrote:
>
> Firstly, my experiments suggest that the layout is only rendered if
> the :layout attribute is included in the call to render. Is my
> understanding correct?

Yes, and it's the way things should be.  I suggest writing a separate
spec for the layout, and testing its behavior separate from the view.
After all, you're *unit* testing here.  And the layout is a separate
unit.


> Secondly, the link_to code only executes if the session contains a
> piece of data (member id). How do I set up the session data in my view
> spec?

With the session[] object, e.g. "session[:foo] = bar".  See:
http://rspec.info/rails/writing/views.html

Whether the view (or layout) should be looking directly into the
session and making decisions from what it sees, instead of trusting
what it gets from the controller, is another question.  "Is there a
current user?" is _probably_ closer to your actual business rules than
"Does the session contain a member id?"



--
Have Fun,
   Steve E. (removed_email_address@domain.invalid)
   ESCAPE POD - The Science Fiction Podcast Magazine
   http://www.escapepod.org
Lee (Guest)
on 2009-06-06 09:40
(Received via mailing list)
Thanks Steve.

As suggested I started to wrtite a separate spec for the layout:

require File.expand_path(File.dirname(__FILE__) + '/../../
spec_helper')

describe "layouts/contexts.html.erb" do

  it "should display a link to create a new zone" do
    render "layouts/contexts.html.erb"
  end

end

When I execute this, it complains of a missing method "sub_menu". This
method, along with others such as "main_menu", are called within the
layout. Unlike "main_menu" which is defined in my
"application_helper.rb" file, "sub_menu" is defined in the helper file
for the controller "contexts". How do I get the spec to "see" this
file and its contents when executed? I think It's seeing "main_menu".

Thanks.
David C. (Guest)
on 2009-06-06 15:10
(Received via mailing list)
On Sat, Jun 6, 2009 at 12:01 AM, Lee <removed_email_address@domain.invalid>
wrote:
>    render "layouts/contexts.html.erb"
>  end
>
> end
>
> When I execute this, it complains of a missing method "sub_menu". This
> method, along with others such as "main_menu", are called within the
> layout. Unlike "main_menu" which is defined in my
> "application_helper.rb" file, "sub_menu" is defined in the helper file
> for the controller "contexts". How do I get the spec to "see" this
> file and its contents when executed? I think It's seeing "main_menu".

http://rspec.rubyforge.org/rspec-rails/1.2.6/class...
Stephen E. (Guest)
on 2009-06-07 03:29
(Received via mailing list)
On Sat, Jun 6, 2009 at 1:01 AM, Lee<removed_email_address@domain.invalid> wrote:
>
> When I execute this, it complains of a missing method "sub_menu". This
> method, along with others such as "main_menu", are called within the
> layout. Unlike "main_menu" which is defined in my
> "application_helper.rb" file, "sub_menu" is defined in the helper file
> for the controller "contexts". How do I get the spec to "see" this
> file and its contents when executed? I think It's seeing "main_menu".

Just require it and then include it.  A "describe" block is really
just a prettified form of class definition, so you can include any
modules you like in it.

Although (warning: continued smartassery here) I'm also wondering why
it's a good idea to call a method defined in a specific controller's
helper from a global layout.  Is this layout only used by that
controller?  If not, why isn't the method in application helpers?



--
Have Fun,
   Steve E. (removed_email_address@domain.invalid)
   ESCAPE POD - The Science Fiction Podcast Magazine
   http://www.escapepod.org
This topic is locked and can not be replied to.