views:

46

answers:

1

I have an application helper which determines the css classes (selected or unselected) and link addresses for the navbar at the top of my application helper.

When I test my controllers, I get a whole bunch of errors regarding the navbar (variables return nil). Since the navbar has nothing to do with each individual controller, and it appears on every page, the easiest thing to do seems to be this.

In the application helper:

def navbar_method(category)
c = Category.find_by_name(category)
    children = c.children.map {|child| child.name}
if category == "All"
    if params[:id] == category || params[:category] == category
        "on"
        else
        "off"
        end
else
    if params[:id] == category || children.include?(params[:id]) || params[:category] == category || children.include?(params[:category])
        "on"
        else
        "off"
    end
end

Here's a snippet of the Navbar view where I employ the method nav_link is another helper method I created similar to nav class. It's a horizontal dropdown menu:

In layouts/application.html.erb:

<li id="Home" class="<%= nav_class("All") %>"><%= nav_link("All") %>
    <ul>
    </ul></li>
    <li id="Writing" class="<%= nav_class("Writing") %>"><%= nav_link("Writing") %>
    <ul>
        <li class="<%= nav_class("Educational") %>"><%= nav_link("Educational") %>

Since everything in this method pops up nil, I just did this:

def navbar_method(category)
unless Rails.env.test?
c = Category.find_by_name(category)
#see above
end
end

What do you think? OK? Not OK?

Since I got one vote for NOT OK I thought I'd explain a little more. For some reason the category is returning nil in the tests despite fixtures I might include...I figured I could do extensive integration tests to test the navbar...

+1  A: 

That's not a good idea.

A functional test should reproduce almost the same environment of an user request. Why the helper doesn't work?

Also, did you setup some unit test to test the helper? Perhaps the helper itself is buggy.

Simone Carletti
Thanks for the advice. I made some edits in the original question so that you could know more about what I'm doing.
Kenji Crosland