Module: Hashblue::API::TestHelper
- Defined in:
- lib/hashblue/api/test_helper.rb
Overview
Test helper for applications using the Hashblue API gem.
Use this helper to stub out calls to the Hashblue API with assurance that stubbing will not allow your application to drift from the current API specification.
Usage
class MyTest < Test::Unit::TestCase
include Hashblue::API::TestHelper
def test_should_work_with_the_api
stub_hashblue_api('123', :messages => [{:content => "blah"}])
subscriber = Hashblue::API::Subscriber.new('123')
assert_equal 1, messages.length
assert_equal "blah", messages[0].content
# etc
The test helper will ensure that the data you are passing to stub_hashblue_api
matches the data that will be returned to your application by the API.
Constant Summary collapse
- API_KEYS =
%w(contact_msisdn content timestamp id sent favourite).map(&:to_sym)
Instance Method Summary collapse
- #stub_hashblue_api(subscriber_id, options = {}) ⇒ Object
- #stub_hashblue_api_message_search(subscriber_id, query, search_results) ⇒ Object
Instance Method Details
#stub_hashblue_api(subscriber_id, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hashblue/api/test_helper.rb', line 29 def stub_hashblue_api(subscriber_id, ={}) .reverse_merge!(:contacts => [], :messages => [], :favourites => []) [:messages] = [:messages].map do |attributes| (subscriber_id, attributes) end favourites = [] [:messages].each do || Hashblue::API.stubs(:delete).with("/subscribers/#{subscriber_id}/messages/#{[:id]}.json") Hashblue::API.stubs(:post).with("/subscribers/#{subscriber_id}/favourites.json", :query => {:id => [:id]}).returns() if [:favourite] == true Hashblue::API.stubs(:delete).with("/subscribers/#{subscriber_id}/favourites/#{[:id]}.json") favourites << end end Hashblue::API.stubs(:post).with("/subscribers/#{subscriber_id}/messages.json", anything).returns( {:content => "sent message", :to=>"123", :sent => true} ) Hashblue::API.stubs(:get).with("/subscribers/#{subscriber_id}/favourites.json", anything).returns(favourites) Hashblue::API.stubs(:get).with("/subscribers/#{subscriber_id}/messages.json", anything).returns([:messages]) contacts = [:messages].group_by { |m| m[:contact_msisdn] }.map do |msisdn, | explicit_contact = [:contacts].find { |c| c[:msisdn] == msisdn } id = explicit_contact ? explicit_contact[:id] : __next_firehose_id url = "/subscribers/#{subscriber_id}/contacts/#{id}/messages.json" Hashblue::API.stubs(:get).with(url, anything).returns() {:id => id, :msisdn => msisdn, :latest_message => .first, :subscriber_id => subscriber_id} end = [:contacts].reject do |contact| [:messages].find { |m| m[:contact_msisdn] == contact[:msisdn] } end.map do |contact| {:latest_message => nil, :id => __next_firehose_id}.merge(contact) end contacts += Hashblue::API.stubs(:get).with("/subscribers/#{subscriber_id}/contacts.json", anything).returns(contacts) end |
#stub_hashblue_api_message_search(subscriber_id, query, search_results) ⇒ Object
76 77 78 79 |
# File 'lib/hashblue/api/test_helper.rb', line 76 def (subscriber_id, query, search_results) Hashblue::API.stubs(:get).with("/subscribers/#{subscriber_id}/messages.json", {:query => {:q => query}}).returns(search_results.map { |m| (subscriber_id, m) }) end |