Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用Test::Unit全局存根http请求?_Ruby On Rails_Testing_Mocking_Twitter_Testunit - Fatal编程技术网

Ruby on rails 如何使用Test::Unit全局存根http请求?

Ruby on rails 如何使用Test::Unit全局存根http请求?,ruby-on-rails,testing,mocking,twitter,testunit,Ruby On Rails,Testing,Mocking,Twitter,Testunit,如何在全局范围内存根http请求(如下面的twitter api),使其对Test::Unit套件中的所有测试都有效 stub_request(:get, "https://api.twitter.com/1/users/show.json?screen_name=digiberber"). with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Twitter Ruby Gem 1.1.2'}).

如何在全局范围内存根http请求(如下面的twitter api),使其对Test::Unit套件中的所有测试都有效

stub_request(:get, "https://api.twitter.com/1/users/show.json?screen_name=digiberber").
    with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Twitter Ruby Gem 1.1.2'}).
    to_return(:status => 200, :body => "", :headers => {})
此存根在TestCase子类的setup()块中工作,如

class MyTest < ActiveSupport::TestCase       
  setup do
    stub_request(...)...
  end
end
这给了我一个错误:

NoMethodError: undefined method `stub_request' for ActiveSupport::TestCase:Class
我还尝试了修补def方法本身

def self.setup
  stub_request(...)
end
但它也不起作用

当我使用WebMock代替WebMock时,也会发生类似的情况。这似乎是一个范围问题,但我不知道如何解决它。想法?

关于设置()和拆卸()的不同方法的想法促使我这样做

class ActiveSupport::TestCase  
  def setup
    stub_request(...)
  end
end
没有想过将其声明为实例方法:P

使用可以执行以下操作:

在*test/test_helper.rb中*

require 'fakeweb'

class ActiveSupport::TestCase
  def setup
    # FakeWeb global setup
    FakeWeb.allow_net_connect = false # force an error if there are a net connection to other than the FakeWeb URIs
    FakeWeb.register_uri(:get, 
        "https://api.twitter.com/1/users/show.json?screen_name=digiberber",
        :body => "",
        :content_type => "application/json")
  end
  def teardown
    FakeWeb.allow_net_connect = true
    FakeWeb.clean_registry # Clear all registered uris
  end
end

这样,您就可以从任何测试用例调用已注册的fakeweb。

水豚驱动程序Akephalos确实支持stubing http调用。他们称之为过滤器


抱歉,刚才回答了,但没有提到您正在使用Test::Unit。无论如何,看看FakeWeb。fakeweb不会有同样的问题吗?register_uri()与webmock的stub_request()非常相似,我还需要全局运行它。也许您可以将HTTP请求抽象为一个类或模块方法,然后您可以轻松地对其进行模拟或存根。@Ogz的向导,实际上这是我最初的方法-在全局设置()下使用flexmock存根twitter gem的twitter.user()。但是flexmock()也没有被识别:P.我认为在这两种方法中,我只是在错误的位置进行存根,但无法找出正确的位置…我的意思是,我只是在错误的位置添加存根调用,在那里模拟库没有被识别。我不知道正确的位置在哪里,也不知道如何强制包含那些存根/模拟方法。例如,尝试了FlexMock.FlexMock(),但FlexMock模块不存在,尽管它是必需的(尝试了include(),但也没有成功)
require 'fakeweb'

class ActiveSupport::TestCase
  def setup
    # FakeWeb global setup
    FakeWeb.allow_net_connect = false # force an error if there are a net connection to other than the FakeWeb URIs
    FakeWeb.register_uri(:get, 
        "https://api.twitter.com/1/users/show.json?screen_name=digiberber",
        :body => "",
        :content_type => "application/json")
  end
  def teardown
    FakeWeb.allow_net_connect = true
    FakeWeb.clean_registry # Clear all registered uris
  end
end