无法对Flickr API发送存根请求

时间:2016-02-10 19:14:51

标签: ruby-on-rails rspec flickr webmock

我无法将请求存根到Flickr API 控制器测试。我使用gem'flickraw'从Flickr API获取数据。

flickr_search_controller.rb:

module Dashboard
  class FlickrSearchController < Dashboard::BaseController
    respond_to :js

    def search
      @search_tag = params[:search]
      photos_list = if @search_tag.blank?
                      flickr.photos.getRecent(per_page: 10)
                    else
                      flickr.photos.search(text: @search_tag, per_page: 10)
                    end

      @photos = photos_list.map { |photo| FlickRaw.url_q(photo) }
    end
  end
end

flickr_search_controller_spec.rb:

require 'rails_helper'

describe Dashboard::FlickrSearchController do
  let(:user) { FactoryGirl.create(:user) }
  before(:each) do
    stub_request(:post, "https://api.flickr.com/services/rest").to_return(status: 200)
    @controller.send(:auto_login, user)
  end

describe 'when user didn\'t set search tag' do
  it 'returns recend photo'do
    get :search, search: ' '
    expect(response.status).to eq(200)
  end
end

端 我进入控制台下一个错误:

Failures:

1) Dashboard::FlickrSearchController when user didn't set search tag returns recend photo
 Failure/Error: flickr.photos.getRecent(per_page: 10)

 WebMock::NetConnectNotAllowedError:
   Real HTTP connections are disabled. Unregistered request: POST https://api.flickr.com/services/rest/ with body 'method=flickr.reflection.getMethods&format=json&nojsoncallback=1' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'OAuth realm="https://api.flickr.com/services/rest/", oauth_consumer_key="32904448e7d40c7e833c7b381c86cd31", oauth_nonce="lCL%2FUM9o8go5XNVy4F7p%2FNxHJrY%2BvFNLhlzueFq8Juc%3D", oauth_signature="1b77fc6af54b2b51%26", oauth_signature_method="PLAINTEXT", oauth_timestamp="1455128674", oauth_token="", oauth_version="1.0"', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'FlickRaw/0.9.8'}

   You can stub this request with the following snippet:

   stub_request(:post, "https://api.flickr.com/services/rest/").
     with(:body => {"format"=>"json", "method"=>"flickr.reflection.getMethods", "nojsoncallback"=>"1"},
          :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'OAuth realm="https://api.flickr.com/services/rest/", oauth_consumer_key="32904448e7d40c7e833c7b381c86cd31", oauth_nonce="lCL%2FUM9o8go5XNVy4F7p%2FNxHJrY%2BvFNLhlzueFq8Juc%3D", oauth_signature="1b77fc6af54b2b51%26", oauth_signature_method="PLAINTEXT", oauth_timestamp="1455128674", oauth_token="", oauth_version="1.0"', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'FlickRaw/0.9.8'}).
     to_return(:status => 200, :body => "", :headers => {})

   ============================================================

有人知道如何存根此请求?

1 个答案:

答案 0 :(得分:0)

请注意uri中的斜线,您正在进行存根的请求不是正在制作的请求