FbGraph::Mock
Facebook Graph API mocking helper, originally used in fb_graph gem.
This gem simply mock HTTP request & response using webmock gem, and returns pre-registered JSON response.
It means you can use fb_graph-mock gem even with kwala, rest-client, or any other code which access to Facebook Graph API.
Installation
gem install fb_graph-mock
Examples
In spec_helper.rb
require 'fb_graph/mock'
include FbGraph::Mock
WebMock.disable_net_connect! # Optional
In foo_spec.rb
it do
mock_graph :get, 'me', 'users/me_private', :access_token => 'access_token' do
user = FbGraph::User.me('access_token').fetch
user.website.should == 'http://matake.jp'
end
end
it do
mock_graph :get, 'me', 'users/me_public', :status => [401, 'Unauthorized'] do
lambda do
FbGraph::User.fetch :me
end.should raise_error FbGraph::Unauthorized
end
end
More Examples?
You can see lots of samples in fb_graph’s specs. github.com/nov/fb_graph
Registered Mocks and Your Custom Mocks
To improve fb_graph and other projects using this gem, keeping mock JSONs are important.
If you find any legacy response format in this gem’s mock_json directory, please report an issue or send a pull request with new JSON file.
You can find all registered responses as below.
FbGraph::Mock.registered_mocks
If it’s hard to share your mock JSON (e.g. because of privacy issue), you can use your own response file as below.
file_path = File.join(Rails.root, 'spec/my_own_mock_json/users/me_private.json')
mock_graph :get, 'me', file_path do
# Do something.
end
If a file is found at the given file path, this gem uses the file.
Otherwise, this gem search a registered mock JSON file in its own registry.
mock_graph :get, 'me', 'users/albums/arjun_public' do
# => uses "mock_json/users/albums/arjun_public.json" in this gem.
# Do something.
end
Note on Patches/Pull Requests
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright
Copyright © 2012 nov matake. See LICENSE for details.