Testing Facebook with Cucumber
For those that haven’t heard: Cucumber is pretty much the greatest thing since sliced bread. It dramatically improved the quality and stability of our applications, and the outside-in approach that it encourages forces you to stay focused on what’s important.
When we started working on a Facebook application a few months ago, we couldn’t fathom not using Cucumber. So we had to figure out a way to test it. It took us a few months to evolve it to a point where we could extract it, but this week we pushed a change to Facebooker to make life a little easier. So grab the latest version of Facebooker and keep on reading…
First, in features/support/env.rb
, replace the default Rails world with the one in Facebooker:
Given I am logged in as a Facebook user
Most of our Facebook application requires that a user be logged in. So most of our scenarios started with “Given I am logged in as a Facebook user”.
Scenario: Uploading a video
Given I am logged in as a Facebook user
When I upload a video
Then I can see a video on my blog
And here is our implementation for that step:
Our application has a User
model with a facebook_id
attribute and a #facebook_user
method which returned an instance of Facebooker::User
. Due to how the Facebook session is being mocked, it is important that we set our fake user’s id to 1 for now (I’ll try to figure out a way around this). We also manually add some friends for our application to use. Lastly, we merge in our user’s id and friend ids into the default request params so that any requests we make include those parameters.
Drop your…canvas
There were some places in our app where requests don’t go through the canvas. For example, we have a few multipart forms, which have to submit directly to your application. To mimic this, wrap webrat calls in #without_canvas
:
Note that if your action redirects to a URL with :canvas => true
, webrat will see that as an “external” redirect and won’t follow it. Just call #follow_redirect!
and it’ll go on it’s merry way.
Accessing the Facebook API
Instead of making requests to the Facebook API in your tests, Facebooker will try to read a canned response fixture from features/support/facebook/
. It will give you a friendly error whenever this happens, so just follow the directions and you should be on your way.
Feedback & Patches
I’m sure there are things that don’t work right, so let me know if you run into any troubles. If you have any ideas for making the Cucumber support better, please share them here or on the Facebooker mailing list.