Testing Facebook application
posted in /home/JavaScript on 2011-11-29 |

I had a lot of work these days and didn't have time to blog here, but I think that the tips in this article are very useful. The number of Facebook applications is increasing every day. We all know that the testing (debugging) of our code is really important. That's why I decided to share my experience in this area.
You can download the full source code of the examples here.
Check the testing of the JS SDK here http://krasimirtsonev.com/files/testfacebook/js/.
Check the testing of the PHP SDK here http://krasimirtsonev.com/files/testfacebook/php/.
1. Creating facebook application
- Go to https://developers.facebook.com/apps
- Click on + Create New App

- Type the name of your application and click Continue

- Copy/remember your App ID and App Secret

- Set your App Domain and Site URL
(I'm using a virtual host of my local machine. As far as I know currently Facebook doesn't allow the usage of multiple domains.)

2. Testing with PHP SDK
For my PHP tests I'm using my own version of SimpleTest. Here is a short script that illustrates the usage of Test class.
And the result is:

So, we have a tool for testing. The second thing that we need is the Facebook's PHP SDK. Download it from here and create a new php with the following content:
Firstly we should check if the configuration is available:
After that we have to initialize the SDK and check if there is a token available:
And finally check if there is a logged Facebook user:
The result of this test when I'm logged in is:

Of course the FacebookTestingCase class could be filled with more methods, which may check something else depending on your custom application's logic.
3. Testing with JavaScript SDK
I really like Jasmine for JavaScript testing. That's why I strongly recommend it and I'm using it for this example.
I think that it is a good idea to wrap the Facebook's stuff in a module. You can use this module in some of your next projects and it is much more flexible for testing.
Nothing fancy. Just simple usage of the SDK. Here it is the full Jasmine test:
The result if you are logged:

As a conclusion I could say that the testing of FB applications is not very pleasant task. Facebook still doesn't provide any information if something goes wrong. It will be good if we (as developers) know what exactly is not ok. For example if we use wrong application ID or wrong App Domain name. We should at least receive some result of our requests. I believe that Facebook are going in the right direction.
Download the full source code of the examples above here.
Check the testing of the JS SDK here http://krasimirtsonev.com/files/testfacebook/js/.
Check the testing of the PHP SDK here http://krasimirtsonev.com/files/testfacebook/php/.
Delicious