2

我需要测试以下助手:

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 

我想知道是否有一种方法可以测试它的输出。基本上,我只是想测试帮助程序是否为我提供了正确数量的 li 元素,并且可能是没有任何课程的情况。

我的第一个想法是做这样的事情:

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

这很好用。但是,如果我将 :count 选项添加到 have_selector 调用它突然失败,有人可以帮我弄清楚为什么会这样吗?

4

3 回答 3

5

我相信您正在寻找的是 have_tag 和 with_tag RSpec 助手

describe DashboardHelper do
  describe display_all_courses do
    it "should return an list of all the courses" do
      7.times{ Factory(:course) }
      helper.display_all_courses.should have_tag('ul') do
        with_tag('li', 3)
      end
    end
  end
end
于 2012-05-25T21:33:45.623 回答
1

也许将html视为xml可能会有所帮助?在这种情况下,此链接可能会有所帮助。

它定义了一个匹配器have_xml,它可能正是您所需要的。虽然我知道如果它也have_tag适用于字符串会更好。

于 2010-08-23T12:08:40.990 回答
-8

显然,模板是做到这一点的最佳方式。

于 2010-09-10T23:34:55.233 回答