views:

88

answers:

2

Guys,

I need to test the following helper:

def display_all_courses
  @courses = Course.all

  output =  ""

  for course in @courses do
    output << content_tag(:li, :id => course.title.gsub(" ", "-").downcase.strip) do
      concat content_tag(:h1, course.title)
      concat link_to("Edit", edit_course_path(course))
    end
  end

  return output
end 

and I'm wondering if there is a way that I can test the output of this. Basically, I just want to test that the helper gets me the correct number of li elements, and maybe the case when there aren't any courses.

My first thought is to do something like this:

describe DashboardHelper do
  describe display_all_courses do
    it "should return an list of all the courses" do
      7.times{Factory(:course)
      html = helper.display_all_courses
      html.should have_selector(:li)
    end
  end
end

and this works just fine. However, if I add the :count option to the have_selector call it suddenly fails, can anybody help me figure out why that is?

A: 

Maybe it could help to treat the html as xml? In that case this link could help.

It defines a matcher have_xml which could be just what you need. Although i understand it would be nicer if the have_tag would just work on strings too.

nathanvda
hmmmm I will look into this, thanks for the heads up.
TheDelChop
A: 

Clearly a template is the best way to do this.

TheDelChop