我有一个非常简单的控制器,可以使用Feedjira从rss获取一些数据。我想通过记录RSS响应来测试这个控制器。以下是控制器代码:
def index
@news = Feedjira::Feed.fetch_and_parse URI.encode("http://news.google.com/news/feeds?q=\"#{query}\"&output=rss")
end还有我的规格测试:
it "should assign news feed", :vcr do
get :index
assigns(:news).entries.size.should == 6
assigns(:news).entries[0].title.should == "First item title"
endvcd配置的代码:
VCR.configure do |c|
c.cassette_library_dir = Rails.root.join("spec", "vcr")
c.hook_into :fakeweb
c.ignore_localhost = true
end
RSpec.configure do |c|
c.treat_symbols_as_metadata_keys_with_true_values = true
c.around(:each, :vcr) do |example|
name = example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_")
options = example.metadata.slice(:record, :match_requests_on).except(:example_group)
VCR.use_cassette(name, options) { example.call }
end
end由于一些未知的原因,VCR盒式磁带在这个特定的测试中没有被记录。所有其他使用web调用的测试都可以工作,但使用Feedjira的这个测试似乎无法检测到网络调用。为什么?
发布于 2014-09-09 14:03:10
根据Feedjira's home page的说法,它使用的是HTTP请求,而不是Net::HTTP:
Feedjira的一个重要目标是通过使用libcurl-
在gem中快速获取速度。
VCR只能使用FakeWeb来挂钩Net::HTTP请求。要挂接到路缘请求中,您需要使用hook_into :webmock。
发布于 2016-11-13 03:59:47
在Feedjira 2.0中的this commit中,Feedjira使用法拉第,这意味着您可以遵循Faraday readme中的测试指南或使用录像机。
Feedjira现在也在内部使用VCR。Example
例如,您可以在rspec示例中使用vcr,
it 'fetches and parses the feed' do
VCR.use_cassette('success') do
feed = Feedjira::Feed.fetch_and_parse 'http://feedjira.com/blog/feed.xml'
expect(feed.last_modified).to eq('Fri, 07 Oct 2016 14:37:00 GMT')
end
endhttps://stackoverflow.com/questions/25515434
复制相似问题