如何测试使用机械化的红宝石应用程序 [英] How to test a ruby application which uses mechanize

查看:54
本文介绍了如何测试使用机械化的红宝石应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小程序,该程序使用Mechanize遍历站点.

I wrote a small program that uses Mechanize to traverse a site.

我想为此编写测试,但不希望它每次运行测试时都实际登录到该站点.我想模拟互联网,以便当它访问某个站点时,它只是返回存储的结果.

I want to write tests for it, but don't want it to actually go log onto the site every time I run the tests. I would like to mock the internet, so that when it goes to some site, it simply returns the stored results.

这里是一个小例子,假设我的代码的目的是从Google主页上拉出链接,所以我编写了一个测试以确保我的代码找到的第一个链接包含文本"Images".我可能会这样写:

Here is a small example, pretend my code's purpose was to pull links off of the google homepage, so I write a test to ensure the first link my code finds has the text "Images". I might write something like this:

require 'rubygems'
require 'mechanize'
require 'test/unit'

def my_code_to_find_links
  google = WWW::Mechanize.new.get('http://www.google.com')
  # ...
  # some code to figure out which liks it wants
  # ...
  google.links
end

class TestGoogle < Test::Unit::TestCase
  def test_first_link_is_images
    assert_equal 'Images' , my_code_to_find_links.first.text
  end
end

如何模拟google.com,这样我就可以测试my_code_to_find_links,而无需实际上接入互联网的所有开销?

How do I mock google.com so that I can test my_code_to_find_links without all the overhead of actually accessing the internet?

谢谢 -乔什

推荐答案

使用Fakeweb存根Internet响应.

Use Fakeweb to stub out the internet responses.

对于Google的示例,首先进入网站并保存所需页面的html.在这种情况下,假设您通过浏览器或curl保存了www.google.com.将Fakeweb gem添加到您的test.rb文件中

For the Google example, first go to the website and save the html of the page you desire. In this case, let's assume you saved www.google.com via your browser or curl. Add the Fakeweb gem to your test.rb file

那么您的代码是

stream = File.read("saved_google_page.html")
FakeWeb.register_uri(:get, 
    "http://www.google.com", 
    :body => stream, 
    :content_type => "text/html")

当您进行标准的机械化调用时,

When you do your standard Mechanize call of

agent = Mechanize.New
page = agent.get("http://www.google.com/")

Fakeweb将以Mechanize认为成功访问Internet的方式返回您保存的带有content_type标头的页面.确保设置了content_type标头,否则Mechanize会将响应视为Mechanize :: File而不是Mechanize :: Page.您可以在拔掉网络连接的情况下在计算机上运行测试,以测试它是否可以正常工作.

Fakeweb will return the page you saved with the content_type headers set in a way that Mechanize will think it accessed the internet successfully. Make sure that content_type header is set since otherwise Mechanize treats the response as Mechanize::File instead of Mechanize::Page. You can test that it's fully working by running tests on your machine with the network connection unplugged.

p.s.我在提出问题后的6个月内回答了这个问题,因为这是Google的最高结果,但尚未得到答案.我只花了30分钟的时间自己弄清楚这个问题,并认为我会分享解决方案.

p.s. I'm answering this 6 months after the question was asked since this is the top result in Google but it's unanswered. I just spent 30 minutes figuring this out myself and thought I'd share the solution.

这篇关于如何测试使用机械化的红宝石应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆