Module: MnoEnterpriseApiTestHelper
- Defined in:
- lib/mno_enterprise/testing_support/mno_enterprise_api_test_helper.rb
Instance Method Summary collapse
-
#api_stub_for(klass, opts = {}) ⇒ Object
Example usage:.
-
#api_stub_reset ⇒ Object
Reset all API stubs.
- #entity_count(res) ⇒ Object
-
#from_api(res) ⇒ Object
Take a resource and transform it into a Hash describing the resource as if it had been returned by the MnoEnterprise API server.
- #serialize_type(res) ⇒ Object
Instance Method Details
#api_stub_for(klass, opts = {}) ⇒ Object
Example usage:
Without opts, it yields a faraday stub object which you can configure manually:
You can also pass the response stub via opts api_stub_for(User,
path: '/users/popular',
response: [{ id: 1, name: "Tobias Fünke" }, { id: 2, name: "Lindsay Fünke" }]
)
You can also specify the response code: api_stub_for(User,
path: '/users/popular',
code: 200,
response: [{ id: 1, name: "Tobias Fünke" }, { id: 2, name: "Lindsay Fünke" }]
)
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mno_enterprise/testing_support/mno_enterprise_api_test_helper.rb', line 71 def api_stub_for(klass, opts = {}) real_opts = klass if klass.is_a?(Class) warn("DEPRECATION WARNING: api_stub_for(MyClass,{ some: 'opts'}) is deprecated. Please use api_stub_for({ some: 'opts' }) from now on") real_opts = opts end set_api_stub api_stub_add(real_opts) api_stub_configure(@_api_stub) end |
#api_stub_reset ⇒ Object
Reset all API stubs. Called before each test (see spec_helper)
48 49 50 51 52 |
# File 'lib/mno_enterprise/testing_support/mno_enterprise_api_test_helper.rb', line 48 def api_stub_reset @_api_stub = nil @_stub_list = {} api_stub_configure(Her::API.new) end |
#entity_count(res) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mno_enterprise/testing_support/mno_enterprise_api_test_helper.rb', line 35 def entity_count(res) case when res.kind_of?(Array) return res.count when res.kind_of?(Hash) return res.count else return 1 end end |
#from_api(res) ⇒ Object
Take a resource and transform it into a Hash describing the resource as if it had been returned by the MnoEnterprise API server
6 7 8 |
# File 'lib/mno_enterprise/testing_support/mno_enterprise_api_test_helper.rb', line 6 def from_api(res) { data: serialize_type(res), metadata: {pagination: {count: entity_count(res)}} } end |
#serialize_type(res) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mno_enterprise/testing_support/mno_enterprise_api_test_helper.rb', line 10 def serialize_type(res) case when res.kind_of?(Array) return res.map { |e| serialize_type(e) } when res.kind_of?(MnoEnterprise::BaseResource) hash = res.attributes.dup hash.each do |k,v| hash[k] = serialize_type(v) end return hash when res.kind_of?(Hash) hash = res.dup hash.each do |k,v| hash[k] = serialize_type(v) end return hash when res.kind_of?(Money) return { cents: res.cents, currency: res.currency_as_string } when res.respond_to?(:iso8601) return res.iso8601 else return res end end |