views:

270

answers:

2

Anyone have any tips for best practices for mocking out facebook requests in functional tests? Is it just as simple as adding all of the proper params to the request? Is there a way to stub those out?

I'm using facebooker, which comes with a mock service:

  # A mock service that reads the Facebook response from fixtures
  # Adapted from http://gist.github.com/44344
  #
  #   Facebooker::MockService.fixture_path = 'path/to/dir'
  #   Facebooker::Session.current = Facebooker::MockSession.create

But when I write a basic get test, it tries to redirect the browser to the facebook page for adding the app, which I assume indicates that the mocking isn't working.

  test "loads respondent" do
    Facebooker::Session.current = Facebooker::MockSession.create
    get :index
    puts @response.body # => <html><body>You are being <a href="http://www.facebook.com/install.php?api_key=65e9d2c74b295cc5bcea935b584557f6&amp;amp;v=1.0&amp;amp;next=http%3A%2F%2Ftest.host%2Ffacebook"&gt;redirected&lt;/a&gt;.&lt;/body&gt;&lt;/html&gt;
  end
A: 

Yes, the way to stub it out is to use Fakeweb. Cheers!

westoque
+3  A: 

I got this working with the latest version of facebooker (1.0.58):

# test_helper.rb
require 'facebooker/mock/session'
require 'facebooker/mock/service'

Facebooker::MockService.fixture_path = File.join(RAILS_ROOT, 'test', 'fixtures', 'facebook')

Obviously you will have to create the facebook directory in fixtures, or put it wherever. Inside you have to add a folder for each facebook method, and an xml file for the different types of responses you want to test for. I had to add facebook.users.getInfo and facebook.users.hasAppPermission. The easiest is just to add a file named default.xml with the example code from the facebook wiki for those actions.

 # Controller test
 test "facebook action" do  
   get :index, {:fb_sig_added => true}, :facebook_session => Facebooker::MockSession.create
   assert_response :success
 end

The fb_sig_added param is necessary as far as I can tell, because the internal facebooker logic checks the params directly before checking the session on that one. Which seems a bit wanky to me but maybe there's a reason for that.

floyd