使用 Ruby Mechanize 时进行存根测试

发布于 2024-09-01 16:45:42 字数 1896 浏览 3 评论 0原文

我一直在尝试使用 Mocha 对使用 Mechanize 的代码进行一些存根测试。下面是一个示例方法:

  def lookup_course subject_area = nil, course = nil, quarter = nil, year = nil
    raise ArgumentError, "Subject Area can not be nil" if (subject_area.nil? || subject_area.empty?)
    page = get_page FIND_BASIC_COURSES
    raise ArgumentError, "Invalid Subject Area" unless page.body.include?(subject_area.upcase)
    form = page.form_with(:action => 'BasicFindCourses.aspx')
    if !quarter.nil? && !quarter.empty? && QUARTERS.has_key?(quarter.downcase) && year.is_a?(Integer)
      form['ctl00$pageContent$quarterDropDown'] = "#{Time.now.year}#{QUARTERS[quarter]}"
      puts form['ctl00$pageContent$quarterDropDown']
    end
    form['ctl00$pageContent$subjectAreaDropDown'] = subject_area
    form['ctl00$pageContent$courseNumberTextBox'] = course if (!course.nil? && !course.empty?)
    result = form.submit(form.button_with(:name => 'ctl00$pageContent$searchButton'))
    result.body.downcase.include?(subject_area.downcase) ? result : false
  end

因此 get_page 方法将返回一个 Mechanize::Page,其中包含已解析的 html 和所有好东西。如果有人知道一种方法来获取该对象并执行诸如序列化之类的操作,我会很高兴(但是,由于 Mechanize::Page 的子模块之一不理解如何从编组进行转储,序列化不起作用对象)。显然由于我对存根缺乏了解,我一直要做的是:

should "return a mechanize page with valid course subject" do
  Mechanize::Page.stubs(:body).returns(FIND_COURSE_HTML)
  Mechanize::Page.stubs(:form_with).returns(message = {})
  Mechanize::Form.stubs(:submit).returns(true)

  assert_equal Mechanize::Page,  @access.lookup_course("CMPSC").class
end

上面的代码是一个正在进行的工作,因为当我这样做时,我意识到必须有一种更好的方法,希望有一个你们中的聪明人已经做过这种事情了。我不想删除所有功能。理想情况下,我希望能够使用 html 创建一个 Mechanize::Page 对象(因为我知道这些页面上的 html 是什么......我认为这将是一个很好的存根)。尽管如此,我还是不知道如何用 html 实例化 Mechanize::Page。

任何人都可以指导我更好的方向来测试像lookup_course 这样使用大量功能的方法。也许我应该打破代码中的逻辑以使其更好(如果是这样,您会如何建议?)

谢谢您的时间,

迈克尔

I've been trying to use Mocha to do some stubbing for tests on code using Mechanize. Here is an example method:

  def lookup_course subject_area = nil, course = nil, quarter = nil, year = nil
    raise ArgumentError, "Subject Area can not be nil" if (subject_area.nil? || subject_area.empty?)
    page = get_page FIND_BASIC_COURSES
    raise ArgumentError, "Invalid Subject Area" unless page.body.include?(subject_area.upcase)
    form = page.form_with(:action => 'BasicFindCourses.aspx')
    if !quarter.nil? && !quarter.empty? && QUARTERS.has_key?(quarter.downcase) && year.is_a?(Integer)
      form['ctl00$pageContent$quarterDropDown'] = "#{Time.now.year}#{QUARTERS[quarter]}"
      puts form['ctl00$pageContent$quarterDropDown']
    end
    form['ctl00$pageContent$subjectAreaDropDown'] = subject_area
    form['ctl00$pageContent$courseNumberTextBox'] = course if (!course.nil? && !course.empty?)
    result = form.submit(form.button_with(:name => 'ctl00$pageContent$searchButton'))
    result.body.downcase.include?(subject_area.downcase) ? result : false
  end

So the get_page method will return a Mechanize::Page with the html parsed and all that good stuff. I'd love it if someone knew a way to take that object and do something like a serialization of it (however, serialize did not work due to one of the sub modules of Mechanize::Page not understanding how to do a dump from Marshalling the object). What I've been having to do, obviously due to my lack in understanding of stubbing, is:

should "return a mechanize page with valid course subject" do
  Mechanize::Page.stubs(:body).returns(FIND_COURSE_HTML)
  Mechanize::Page.stubs(:form_with).returns(message = {})
  Mechanize::Form.stubs(:submit).returns(true)

  assert_equal Mechanize::Page,  @access.lookup_course("CMPSC").class
end

The above code is a work-in-progress, because as I was doing it I realized there HAS to be a better way and hopefully one of you smart guys out there has already done this type of thing. I don't want to have to stub out every bit of functionality. Ideally, I'd like to be able to create a Mechanize::Page object with html (since I'll know what the html will be on these pages... and that would be a good stub I think). None the less, I could not figure out how to instantiate a Mechanize::Page with html.

Could anyone guide me in a better direction to testing a method like lookup_course that uses lots of pieces of functionality. Maybe I should break up the logic in my code to make this better (if so, how would you suggest?)

Thank you for your time,

Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦 2024-09-08 16:45:42

您可以看看这个 Stakoverflow 问题,其中建议使用 Fakeweb。另一个值得一试的是 Webmock

You could take a look at this Stakoverflow questions which suggests using Fakeweb. Another one worth checking out is Webmock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文