hanklords / flickraw

Ruby library to access flickr api.
http://hanklords.github.com/flickraw/
MIT License
507 stars 98 forks source link

Can't stub request to Flickr API then I try test my controller. #88

Closed alekgrischenko closed 6 years ago

alekgrischenko commented 8 years ago

Hello, I can't stub request to Flickr API for my controller test 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
end

I get in console next error:

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 => {})

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

Does somebody have idea how can I stub this request?

alekgrischenko commented 8 years ago

I bypassed it using cached version flickraw.

jkraemer commented 8 years ago

Create your own flickr client class and in there, use Flickraw. You can then easily replace that implementation for testing with a mock client that doesn't use Flickraw but returns fake responses.