diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5042da7..2fc7d49 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-05-10 19:37:12 -0400 using RuboCop version 0.81.0. +# on 2020-05-14 17:08:39 -0400 using RuboCop version 0.81.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -59,7 +59,7 @@ Style/MethodMissingSuper: Exclude: - 'lib/hyperclient/collection.rb' -# Offense count: 91 +# Offense count: 94 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https diff --git a/.travis.yml b/.travis.yml index 18b789e..08402d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,21 +5,20 @@ sudo: false matrix: include: - rvm: 2.6.6 - - rvm: 2.5.5 - - rvm: 2.4.6 - - rvm: 2.4.6 + env: FARADAY_VERSION=0.9.0 + - rvm: 2.6.6 + env: FARADAY_VERSION=0.17.0 + - rvm: 2.6.6 + env: FARADAY_VERSION="~> 1.0" + - rvm: 2.6.6 script: - bundle exec danger + - rvm: 2.3.8 - rvm: jruby-9.2.7.0 - rvm: jruby-head - - rvm: 2.3.8 - rvm: ruby-head allow_failures: - rvm: ruby-head - rvm: jruby-head -before_install: - - gem update --system - - gem install bundler - bundler_args: --without development diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e55eab..e11ceb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### 0.9.4 (Next) +* [#163](https://github.com/codegram/hyperclient/pull/163): Test against Faraday 0.9, 0.17 and 1.0+ - [@dblock](https://github.com/dblock). * Your contribution here. ### 0.9.3 (2020/05/14) diff --git a/Gemfile b/Gemfile index 31b7b16..23992ca 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,8 @@ git_source(:github) { |repo| "https://github.com/#{repo['/'] ? repo : "#{repo}/# source 'https://rubygems.org' +gem 'faraday', ENV['FARADAY_VERSION'] if ENV.key?('FARADAY_VERSION') + gemspec group :development do diff --git a/test/faraday/connection_test.rb b/test/faraday/connection_test.rb index b2e705d..55c4d99 100644 --- a/test/faraday/connection_test.rb +++ b/test/faraday/connection_test.rb @@ -14,7 +14,7 @@ module Faraday it 'inserts the DigestAuth middleware at the top' do connection.digest_auth('user', 'password') - connection.builder.handlers.first.klass.must_equal Faraday::Request::DigestAuth + _(connection.builder.handlers.first.klass).must_equal Faraday::Request::DigestAuth end it 'passes the user and password to the middleware' do diff --git a/test/hyperclient/attributes_test.rb b/test/hyperclient/attributes_test.rb index 4a176dd..5c0587f 100644 --- a/test/hyperclient/attributes_test.rb +++ b/test/hyperclient/attributes_test.rb @@ -12,29 +12,29 @@ module Hyperclient end it 'does not set _links as an attribute' do - attributes.wont_respond_to :_links + _(attributes).wont_respond_to :_links end it 'does not set _embedded as an attribute' do - attributes.wont_respond_to :_embedded + _(attributes).wont_respond_to :_embedded end it 'sets normal attributes' do - attributes.must_respond_to :permitted - attributes.permitted.must_equal true + _(attributes).must_respond_to :permitted + _(attributes.permitted).must_equal true - attributes.must_respond_to :title - attributes.title.must_equal 'Real World ASP.NET MVC3' + _(attributes).must_respond_to :title + _(attributes.title).must_equal 'Real World ASP.NET MVC3' end # Underscores should be allowed per http://tools.ietf.org/html/draft-kelly-json-hal#appendix-B.4 it 'sets _hidden_attribute as an attribute' do - attributes.must_respond_to :_hidden_attribute - attributes._hidden_attribute.must_equal 'useful value' + _(attributes).must_respond_to :_hidden_attribute + _(attributes._hidden_attribute).must_equal 'useful value' end it 'is a collection' do - Attributes.ancestors.must_include Collection + _(Attributes.ancestors).must_include Collection end end end diff --git a/test/hyperclient/collection_test.rb b/test/hyperclient/collection_test.rb index fe9b58d..290956c 100644 --- a/test/hyperclient/collection_test.rb +++ b/test/hyperclient/collection_test.rb @@ -12,19 +12,19 @@ module Hyperclient end it 'exposes the collection as methods' do - collection.title.must_equal 'Real World ASP.NET MVC3' - collection.description.must_match(/production/) - collection.permitted.must_equal true + _(collection.title).must_equal 'Real World ASP.NET MVC3' + _(collection.description).must_match(/production/) + _(collection.permitted).must_equal true end it 'exposes collection as a hash' do - collection['title'].must_equal 'Real World ASP.NET MVC3' - collection['description'].must_match(/production/) - collection['permitted'].must_equal true + _(collection['title']).must_equal 'Real World ASP.NET MVC3' + _(collection['description']).must_match(/production/) + _(collection['permitted']).must_equal true end it 'correctly responds to methods' do - collection.must_respond_to :title + _(collection).must_respond_to :title end it 'acts as enumerable' do @@ -32,41 +32,41 @@ module Hyperclient name end - names.must_equal %w[_links title description permitted _hidden_attribute _embedded] + _(names).must_equal %w[_links title description permitted _hidden_attribute _embedded] end describe '#to_hash' do it 'returns the wrapped collection as a hash' do - collection.to_hash.must_be_kind_of Hash + _(collection.to_hash).must_be_kind_of Hash end end describe 'include?' do it 'returns true for keys that exist' do - collection.include?('_links').must_equal true + _(collection.include?('_links')).must_equal true end it 'returns false for missing keys' do - collection.include?('missing key').must_equal false + _(collection.include?('missing key')).must_equal false end end describe '#fetch' do it 'returns the value for keys that exist' do - collection.fetch('title').must_equal 'Real World ASP.NET MVC3' + _(collection.fetch('title')).must_equal 'Real World ASP.NET MVC3' end it 'raises an error for missing keys' do - proc { collection.fetch('missing key') }.must_raise KeyError + _(proc { collection.fetch('missing key') }).must_raise KeyError end describe 'with a default value' do it 'returns the value for keys that exist' do - collection.fetch('title', 'default').must_equal 'Real World ASP.NET MVC3' + _(collection.fetch('title', 'default')).must_equal 'Real World ASP.NET MVC3' end it 'returns the default value for missing keys' do - collection.fetch('missing key', 'default').must_equal 'default' + _(collection.fetch('missing key', 'default')).must_equal 'default' end end end diff --git a/test/hyperclient/curie_test.rb b/test/hyperclient/curie_test.rb index 23864fc..0f7cfb4 100644 --- a/test/hyperclient/curie_test.rb +++ b/test/hyperclient/curie_test.rb @@ -11,13 +11,13 @@ module Hyperclient it 'returns true if the curie is templated' do curie = Curie.new({ 'name' => 'image', 'templated' => true }, entry_point) - curie.templated?.must_equal true + _(curie.templated?).must_equal true end it 'returns false if the curie is not templated' do curie = Curie.new({ 'name' => 'image' }, entry_point) - curie.templated?.must_equal false + _(curie.templated?).must_equal false end end @@ -26,12 +26,12 @@ module Hyperclient end describe '_name' do it 'returns curie name' do - curie.name.must_equal 'image' + _(curie.name).must_equal 'image' end end describe 'expand' do it 'expands link' do - curie.expand('thumbnail').must_equal '/images/thumbnail' + _(curie.expand('thumbnail')).must_equal '/images/thumbnail' end end end diff --git a/test/hyperclient/entry_point_test.rb b/test/hyperclient/entry_point_test.rb index 1805e07..7dcd3ce 100644 --- a/test/hyperclient/entry_point_test.rb +++ b/test/hyperclient/entry_point_test.rb @@ -10,60 +10,52 @@ module Hyperclient describe 'connection' do it 'creates a Faraday connection with the entry point url' do - entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/' + _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/' end it 'creates a Faraday connection with the default headers' do - entry_point.headers['Content-Type'].must_equal 'application/hal+json' - entry_point.headers['Accept'].must_equal 'application/hal+json,application/json' + _(entry_point.headers['Content-Type']).must_equal 'application/hal+json' + _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json' end it 'can update headers after a connection has been constructed' do - entry_point.connection.must_be_kind_of Faraday::Connection + _(entry_point.connection).must_be_kind_of Faraday::Connection entry_point.headers.update('Content-Type' => 'application/foobar') - entry_point.headers['Content-Type'].must_equal 'application/foobar' + _(entry_point.headers['Content-Type']).must_equal 'application/foobar' end it 'can insert additional middleware after a connection has been constructed' do - entry_point.connection.must_be_kind_of Faraday::Connection - - warning = 'WARNING: Unexpected middleware set after the adapter. ' \ - "This won't be supported from Faraday 1.0.\n" - - assert_output nil, warning do - entry_point.connection.use :instrumentation - end - + _(entry_point.connection).must_be_kind_of Faraday::Connection + entry_point.connection.use :instrumentation handlers = entry_point.connection.builder.handlers - handlers.must_include FaradayMiddleware::Instrumentation + _(handlers).must_include FaradayMiddleware::Instrumentation end it 'creates a Faraday connection with the default block' do handlers = entry_point.connection.builder.handlers - handlers.must_include Faraday::Response::RaiseError - handlers.must_include FaradayMiddleware::FollowRedirects - handlers.must_include FaradayMiddleware::EncodeHalJson - handlers.must_include FaradayMiddleware::ParseHalJson - handlers.must_include Faraday::Adapter::NetHttp + _(handlers).must_include Faraday::Response::RaiseError + _(handlers).must_include FaradayMiddleware::FollowRedirects + _(handlers).must_include FaradayMiddleware::EncodeHalJson + _(handlers).must_include FaradayMiddleware::ParseHalJson - entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder + _(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder end it 'raises a ConnectionAlreadyInitializedError if attempting to modify headers' do - entry_point.connection.must_be_kind_of Faraday::Connection - -> { entry_point.headers = {} }.must_raise ConnectionAlreadyInitializedError + _(entry_point.connection).must_be_kind_of Faraday::Connection + _(-> { entry_point.headers = {} }).must_raise ConnectionAlreadyInitializedError end it 'raises a ConnectionAlreadyInitializedError if attempting to modify the faraday block' do - entry_point.connection.must_be_kind_of Faraday::Connection - -> { entry_point.connection {} }.must_raise ConnectionAlreadyInitializedError + _(entry_point.connection).must_be_kind_of Faraday::Connection + _(-> { entry_point.connection {} }).must_raise ConnectionAlreadyInitializedError end end describe 'initialize' do it 'sets a Link with the entry point url' do - entry_point._url.must_equal 'http://my.api.org' + _(entry_point._url).must_equal 'http://my.api.org' end end end @@ -77,17 +69,17 @@ module Hyperclient describe 'connection' do it 'creates a Faraday connection with the entry point url' do - entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/' + _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/' end it 'creates a Faraday connection with the default headers' do - entry_point.headers['Content-Type'].must_equal 'application/hal+json' - entry_point.headers['Accept'].must_equal 'application/hal+json,application/json' + _(entry_point.headers['Content-Type']).must_equal 'application/hal+json' + _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json' end it 'creates a Faraday connection with options' do - entry_point.connection.proxy.must_be_kind_of Faraday::ProxyOptions - entry_point.connection.proxy.uri.to_s.must_equal 'http://my.proxy:8080' + _(entry_point.connection.proxy).must_be_kind_of Faraday::ProxyOptions + _(entry_point.connection.proxy.uri.to_s).must_equal 'http://my.proxy:8080' end end end @@ -101,17 +93,17 @@ module Hyperclient describe 'connection' do it 'creates a Faraday connection with the entry point url' do - entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/' + _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/' end it 'creates a Faraday connection with the default headers' do - entry_point.headers['Content-Type'].must_equal 'application/hal+json' - entry_point.headers['Accept'].must_equal 'application/hal+json,application/json' + _(entry_point.headers['Content-Type']).must_equal 'application/hal+json' + _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json' end it 'creates a Faraday connection with options' do - entry_point.connection.proxy.must_be_kind_of Faraday::ProxyOptions - entry_point.connection.proxy.uri.to_s.must_equal 'http://my.proxy:8080' + _(entry_point.connection.proxy).must_be_kind_of Faraday::ProxyOptions + _(entry_point.connection.proxy.uri.to_s).must_equal 'http://my.proxy:8080' end end end @@ -134,21 +126,20 @@ module Hyperclient describe 'connection' do it 'creates a Faraday connection with the entry point url' do - entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/' + _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/' end it 'creates a Faraday connection with non-default headers' do - entry_point.headers['Content-Type'].must_equal 'application/foobar' - entry_point.headers['Accept'].must_equal 'application/foobar' + _(entry_point.headers['Content-Type']).must_equal 'application/foobar' + _(entry_point.headers['Accept']).must_equal 'application/foobar' end it 'creates a Faraday connection with the default block' do handlers = entry_point.connection.builder.handlers - handlers.wont_include Faraday::Response::RaiseError - handlers.wont_include FaradayMiddleware::FollowRedirects - handlers.must_include FaradayMiddleware::EncodeJson - handlers.must_include FaradayMiddleware::ParseJson - handlers.must_include Faraday::Adapter::NetHttp + _(handlers).wont_include Faraday::Response::RaiseError + _(handlers).wont_include FaradayMiddleware::FollowRedirects + _(handlers).must_include FaradayMiddleware::EncodeJson + _(handlers).must_include FaradayMiddleware::ParseJson end end end @@ -165,26 +156,25 @@ module Hyperclient describe 'connection' do it 'creates a Faraday connection with the default and additional headers' do - entry_point.headers['Content-Type'].must_equal 'application/hal+json' - entry_point.headers['Accept'].must_equal 'application/hal+json,application/json' - entry_point.headers['Access-Token'].must_equal 'token' + _(entry_point.headers['Content-Type']).must_equal 'application/hal+json' + _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json' + _(entry_point.headers['Access-Token']).must_equal 'token' end it 'creates a Faraday connection with the entry point url' do - entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/' + _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/' end it 'creates a Faraday connection with the default block plus any additional handlers' do handlers = entry_point.connection.builder.handlers - handlers.must_include Faraday::Request::OAuth - handlers.must_include Faraday::Response::RaiseError - handlers.must_include FaradayMiddleware::FollowRedirects - handlers.must_include FaradayMiddleware::EncodeHalJson - handlers.must_include FaradayMiddleware::ParseHalJson - handlers.must_include Faraday::Adapter::NetHttp + _(handlers).must_include Faraday::Request::OAuth + _(handlers).must_include Faraday::Response::RaiseError + _(handlers).must_include FaradayMiddleware::FollowRedirects + _(handlers).must_include FaradayMiddleware::EncodeHalJson + _(handlers).must_include FaradayMiddleware::ParseHalJson - entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder + _(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder end end end diff --git a/test/hyperclient/link_collection_test.rb b/test/hyperclient/link_collection_test.rb index 501add2..3dcb55d 100644 --- a/test/hyperclient/link_collection_test.rb +++ b/test/hyperclient/link_collection_test.rb @@ -14,33 +14,33 @@ module Hyperclient end it 'is a collection' do - LinkCollection.ancestors.must_include Collection + _(LinkCollection.ancestors).must_include Collection end it 'initializes the collection with links' do - links.must_respond_to :search - links.must_respond_to :gizmos + _(links).must_respond_to :search + _(links).must_respond_to :gizmos end it 'returns link objects for each link' do - links.search.must_be_kind_of Link - links['self'].must_be_kind_of Link + _(links.search).must_be_kind_of Link + _(links['self']).must_be_kind_of Link - links.gizmos.must_be_kind_of Array - links['gizmos'].must_be_kind_of Array + _(links.gizmos).must_be_kind_of Array + _(links['gizmos']).must_be_kind_of Array end describe 'plain link' do let(:plain_link) { links.self } it 'must be correct' do - plain_link._url.must_equal '/productions/1' + _(plain_link._url).must_equal '/productions/1' end end describe 'templated link' do let(:templated_link) { links.search } it 'must expand' do - templated_link._expand(search: 'gizmos')._url.must_equal '/productions/1?categories=gizmos' + _(templated_link._expand(search: 'gizmos')._url).must_equal '/productions/1?categories=gizmos' end end @@ -48,10 +48,10 @@ module Hyperclient let(:curied_link) { links['image:thumbnail'] } let(:curie) { links._curies['image'] } it 'must expand' do - curied_link._expand(version: 'small')._url.must_equal '/images/thumbnails/small.jpg' + _(curied_link._expand(version: 'small')._url).must_equal '/images/thumbnails/small.jpg' end it 'exposes curie' do - curie.expand('thumbnail').must_equal '/docs/images/thumbnail' + _(curie.expand('thumbnail')).must_equal '/docs/images/thumbnail' end end @@ -59,12 +59,12 @@ module Hyperclient let(:gizmos) { links.gizmos } it 'should have 2 items' do - gizmos.length.must_equal 2 + _(gizmos.length).must_equal 2 end it 'must be an array of Links' do gizmos.each do |link| - link.must_be_kind_of Link + _(link).must_be_kind_of Link end end end @@ -72,7 +72,7 @@ module Hyperclient describe 'null link value' do let(:null_link) { links.null_link } it 'must be nil' do - null_link.must_be_nil + _(null_link).must_be_nil end end end diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 47a3785..e4ffeca 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -11,12 +11,12 @@ module Hyperclient describe prop do it 'returns the property value' do link = Link.new('key', { prop => 'value' }, entry_point) - link.send("_#{prop}").must_equal 'value' + _(link.send("_#{prop}")).must_equal 'value' end it 'returns nil if the property is not present' do link = Link.new('key', {}, entry_point) - link.send("_#{prop}").must_be_nil + _(link.send("_#{prop}")).must_be_nil end end end @@ -25,13 +25,13 @@ module Hyperclient it 'returns true if the link is templated' do link = Link.new('key', { 'templated' => true }, entry_point) - link._templated?.must_equal true + _(link._templated?).must_equal true end it 'returns false if the link is not templated' do link = Link.new('key', {}, entry_point) - link._templated?.must_equal false + _(link._templated?).must_equal false end end @@ -39,13 +39,13 @@ module Hyperclient it 'returns a list of required variables' do link = Link.new('key', { 'href' => '/orders{?id,owner}', 'templated' => true }, entry_point) - link._variables.must_equal %w[id owner] + _(link._variables).must_equal %w[id owner] end it 'returns an empty array for untemplated links' do link = Link.new('key', { 'href' => '/orders' }, entry_point) - link._variables.must_equal [] + _(link._variables).must_equal [] end end @@ -53,41 +53,41 @@ module Hyperclient describe 'required argument' do it 'builds a Link with the templated URI representation' do link = Link.new('key', { 'href' => '/orders/{id}', 'templated' => true }, entry_point) - link._expand(id: '1')._url.must_equal '/orders/1' + _(link._expand(id: '1')._url).must_equal '/orders/1' end it 'expands an uri template without variables' do link = Link.new('key', { 'href' => '/orders/{id}', 'templated' => true }, entry_point) - link._expand._url.must_equal '/orders/' - link._url.must_equal '/orders/' + _(link._expand._url).must_equal '/orders/' + _(link._url).must_equal '/orders/' end end describe 'query string argument' do it 'builds a Link with the templated URI representation' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) - link._expand(id: '1')._url.must_equal '/orders?id=1' + _(link._expand(id: '1')._url).must_equal '/orders?id=1' end it 'expands an uri template without variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) - link._expand._url.must_equal '/orders' - link._url.must_equal '/orders' + _(link._expand._url).must_equal '/orders' + _(link._url).must_equal '/orders' end it 'does not expand unknown variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) - link._expand(unknown: '1')._url.must_equal '/orders' + _(link._expand(unknown: '1')._url).must_equal '/orders' end it 'only expands known variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) - link._expand(unknown: '1', id: '2')._url.must_equal '/orders?id=2' + _(link._expand(unknown: '1', id: '2')._url).must_equal '/orders?id=2' end it 'only expands templated links' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => false }, entry_point) - link._expand(id: '1')._url.must_equal '/orders{?id}' + _(link._expand(id: '1')._url).must_equal '/orders{?id}' end end end @@ -96,36 +96,36 @@ module Hyperclient it 'expands an uri template without variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point) - link._url.must_equal '/orders' + _(link._url).must_equal '/orders' end it 'expands an uri template with variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point, id: 1) - link._url.must_equal '/orders?id=1' + _(link._url).must_equal '/orders?id=1' end it 'does not expand an uri template with unknown variables' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point, unknown: 1) - link._url.must_equal '/orders' + _(link._url).must_equal '/orders' end it 'only expands known variables in a uri template' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point, unknown: 1, id: 2) - link._url.must_equal '/orders?id=2' + _(link._url).must_equal '/orders?id=2' end it 'returns the link when no uri template' do link = Link.new('key', { 'href' => '/orders' }, entry_point) - link._url.must_equal '/orders' + _(link._url).must_equal '/orders' end it 'aliases to_s to _url' do link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point, id: 1) - link.to_s.must_equal '/orders?id=1' + _(link.to_s).must_equal '/orders?id=1' end end @@ -151,7 +151,7 @@ module Hyperclient stub.get('http://api.example.org/productions/1') { [200, {}, nil] } end - link._get.must_be_kind_of Resource + _(link._get).must_be_kind_of Resource end it 'raises exceptions by default' do @@ -161,7 +161,7 @@ module Hyperclient stub.get('http://api.example.org/productions/1') { [400, {}, nil] } end - -> { link._get }.must_raise Faraday::ClientError + _(-> { link._get }).must_raise Faraday::ClientError end end @@ -173,7 +173,7 @@ module Hyperclient stub.options('http://api.example.org/productions/1') { [200, {}, nil] } end - link._options.must_be_kind_of Resource + _(link._options).must_be_kind_of Resource end end @@ -185,7 +185,7 @@ module Hyperclient stub.head('http://api.example.org/productions/1') { [200, {}, nil] } end - link._head.must_be_kind_of Resource + _(link._head).must_be_kind_of Resource end end @@ -197,7 +197,7 @@ module Hyperclient stub.delete('http://api.example.org/productions/1') { [200, {}, nil] } end - link._delete.must_be_kind_of Resource + _(link._delete).must_be_kind_of Resource end end @@ -209,7 +209,7 @@ module Hyperclient stub.post('http://api.example.org/productions/1') { [200, {}, nil] } end - link._post('foo' => 'bar').must_be_kind_of Resource + _(link._post('foo' => 'bar')).must_be_kind_of Resource end it 'defaults params to an empty hash' do @@ -217,7 +217,7 @@ module Hyperclient stub.post('http://api.example.org/productions/1') { [200, {}, nil] } end - link._post.must_be_kind_of Resource + _(link._post).must_be_kind_of Resource end end @@ -229,7 +229,7 @@ module Hyperclient stub.put('http://api.example.org/productions/1', '{"foo":"bar"}') { [200, {}, nil] } end - link._put('foo' => 'bar').must_be_kind_of Resource + _(link._put('foo' => 'bar')).must_be_kind_of Resource end it 'defaults params to an empty hash' do @@ -237,7 +237,7 @@ module Hyperclient stub.put('http://api.example.org/productions/1') { [200, {}, nil] } end - link._put.must_be_kind_of Resource + _(link._put).must_be_kind_of Resource end end @@ -249,7 +249,7 @@ module Hyperclient stub.patch('http://api.example.org/productions/1', '{"foo":"bar"}') { [200, {}, nil] } end - link._patch('foo' => 'bar').must_be_kind_of Resource + _(link._patch('foo' => 'bar')).must_be_kind_of Resource end it 'defaults params to an empty hash' do @@ -257,7 +257,7 @@ module Hyperclient stub.patch('http://api.example.org/productions/1') { [200, {}, nil] } end - link._patch.must_be_kind_of Resource + _(link._patch).must_be_kind_of Resource end end @@ -265,8 +265,8 @@ module Hyperclient it 'outputs a custom-friendly output' do link = Link.new('key', { 'href' => '/productions/1' }, 'foo') - link.inspect.must_include 'Link' - link.inspect.must_include '"href"=>"/productions/1"' + _(link.inspect).must_include 'Link' + _(link.inspect).must_include '"href"=>"/productions/1"' end end @@ -279,8 +279,8 @@ module Hyperclient stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] } end - resource.orders._embedded.orders.first.id.must_equal 1 - resource.orders.first.id.must_equal 1 + _(resource.orders._embedded.orders.first.id).must_equal 1 + _(resource.orders.first.id).must_equal 1 end it 'can handle false values in the response' do @@ -290,7 +290,7 @@ module Hyperclient stub.get('http://api.example.org/orders') { [200, {}, { 'any' => false }] } end - resource.orders.any.must_equal false + _(resource.orders.any).must_equal false end it "doesn't delegate when link key doesn't match" do @@ -300,8 +300,8 @@ module Hyperclient stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] } end - resource.foos._embedded.orders.first.id.must_equal 1 - resource.foos.first.must_be_nil + _(resource.foos._embedded.orders.first.id).must_equal 1 + _(resource.foos.first).must_be_nil end it 'backtracks when navigating links' do @@ -311,7 +311,7 @@ module Hyperclient stub.get('http://api.example.org/page2') { [200, {}, { '_links' => { 'next' => { 'href' => 'http://api.example.org/page3' } } }] } end - resource.next._links.next._url.must_equal 'http://api.example.org/page3' + _(resource.next._links.next._url).must_equal 'http://api.example.org/page3' end end @@ -335,18 +335,18 @@ module Hyperclient end it 'raises an error when the method does not exist in the resource' do - -> { link.this_method_does_not_exist }.must_raise NoMethodError + _(-> { link.this_method_does_not_exist }).must_raise NoMethodError end it 'responds to missing methods' do resource.expects(:respond_to?).with('orders').returns(false) resource.expects(:respond_to?).with('embedded').returns(true) - link.respond_to?(:embedded).must_equal true + _(link.respond_to?(:embedded)).must_equal true end it 'does not delegate to_ary to resource' do resource.expects(:to_ary).never - [[link, link]].flatten.must_equal [link, link] + _([[link, link]].flatten).must_equal [link, link] end end end diff --git a/test/hyperclient/resource_collection_test.rb b/test/hyperclient/resource_collection_test.rb index b96ddcc..44a1faa 100644 --- a/test/hyperclient/resource_collection_test.rb +++ b/test/hyperclient/resource_collection_test.rb @@ -14,21 +14,21 @@ module Hyperclient end it 'is a collection' do - ResourceCollection.ancestors.must_include Collection + _(ResourceCollection.ancestors).must_include Collection end it 'initializes the collection with resources' do - resources.must_respond_to :author - resources.must_respond_to :episodes + _(resources).must_respond_to :author + _(resources).must_respond_to :episodes end it 'returns resource objects for each resource' do - resources.author.must_be_kind_of Resource + _(resources.author).must_be_kind_of Resource end it 'also builds arras of resource' do - resources.episodes.must_be_kind_of Array - resources.episodes.first.must_be_kind_of Resource + _(resources.episodes).must_be_kind_of Array + _(resources.episodes.first).must_be_kind_of Resource end end end diff --git a/test/hyperclient/resource_test.rb b/test/hyperclient/resource_test.rb index 03925a4..ce96ae7 100644 --- a/test/hyperclient/resource_test.rb +++ b/test/hyperclient/resource_test.rb @@ -29,7 +29,7 @@ module Hyperclient resource = Resource.new(mock_response.body, entry_point, mock_response) - resource._response.must_equal mock_response + _(resource._response).must_equal mock_response end it 'does not mutate the response.body' do @@ -38,7 +38,7 @@ module Hyperclient resource = Resource.new(mock_response.body, entry_point, mock_response) - resource._response.body.must_equal body + _(resource._response.body).must_equal body end describe 'with an empty body in response' do @@ -47,13 +47,13 @@ module Hyperclient resource = Resource.new(mock_response.body, entry_point, mock_response) - resource._response.must_equal mock_response + _(resource._response).must_equal mock_response end end describe 'with an invalid representation' do it 'raises an InvalidRepresentationError' do - proc { Resource.new('invalid representation data', entry_point) }.must_raise InvalidRepresentationError + _(proc { Resource.new('invalid representation data', entry_point) }).must_raise InvalidRepresentationError end end end @@ -61,9 +61,9 @@ module Hyperclient describe '_links' do it '_expand' do resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders/{id}', 'templated' => true } } }, entry_point) - resource._links.orders._expand(id: 1)._url.must_equal '/orders/1' - resource.orders._expand(id: 1)._url.must_equal '/orders/1' - resource.orders(id: 1)._url.must_equal '/orders/1' + _(resource._links.orders._expand(id: 1)._url).must_equal '/orders/1' + _(resource.orders._expand(id: 1)._url).must_equal '/orders/1' + _(resource.orders(id: 1)._url).must_equal '/orders/1' end end @@ -74,84 +74,84 @@ module Hyperclient describe 'links' do it 'returns a LinkCollection' do - resource._links.must_be_kind_of LinkCollection + _(resource._links).must_be_kind_of LinkCollection end end describe 'attributes' do it 'returns a Attributes' do - resource._attributes.must_be_kind_of Attributes + _(resource._attributes).must_be_kind_of Attributes end end describe 'embedded' do it 'returns a ResourceCollection' do - resource._embedded.must_be_kind_of ResourceCollection + _(resource._embedded).must_be_kind_of ResourceCollection end end describe 'method_missing' do it 'delegates to attributes' do resource._attributes.expects(:foo).returns('bar') - resource.foo.must_equal 'bar' + _(resource.foo).must_equal 'bar' end it 'delegates to links' do resource._links.expects(:foo).returns('bar') - resource.foo.must_equal 'bar' + _(resource.foo).must_equal 'bar' end it 'delegates to embedded' do resource._embedded.expects(:foo).returns('bar') - resource.foo.must_equal 'bar' + _(resource.foo).must_equal 'bar' end it 'delegates to attributes, links, embedded' do resource._attributes.expects('respond_to?').with('foo').returns(false) resource._links.expects('respond_to?').with('foo').returns(false) resource._embedded.expects('respond_to?').with('foo').returns(false) - -> { resource.foo }.must_raise NoMethodError + _(-> { resource.foo }).must_raise NoMethodError end it 'delegates []' do resource._attributes.expects(:foo).returns('bar') - resource['foo'].must_equal 'bar' + _(resource['foo']).must_equal 'bar' end describe '#fetch' do it 'returns the value for keys that exist' do resource._attributes.expects(:foo).returns('bar') - resource.fetch('foo').must_equal 'bar' + _(resource.fetch('foo')).must_equal 'bar' end it 'raises an error for missing keys' do - proc { resource.fetch('missing key') }.must_raise KeyError + _(proc { resource.fetch('missing key') }).must_raise KeyError end describe 'with a default value' do it 'returns the value for keys that exist' do resource._attributes.expects(:foo).returns('bar') - resource.fetch('foo', 'default value').must_equal 'bar' + _(resource.fetch('foo', 'default value')).must_equal 'bar' end it 'returns the default value for missing keys' do - resource.fetch('missing key', 'default value').must_equal 'default value' + _(resource.fetch('missing key', 'default value')).must_equal 'default value' end end describe 'with a block' do it 'returns the value for keys that exist' do resource._attributes.expects(:foo).returns('bar') - resource.fetch('foo') { 'default value' }.must_equal 'bar' + _(resource.fetch('foo') { 'default value' }).must_equal 'bar' end it 'returns the value from the block' do - resource.fetch('z') { 'go fish!' }.must_equal 'go fish!' + _(resource.fetch('z') { 'go fish!' }).must_equal 'go fish!' end it 'returns the value with args from the block' do - resource.fetch('z') { |el| "go fish, #{el}" }.must_equal 'go fish, z' + _(resource.fetch('z') { |el| "go fish, #{el}" }).must_equal 'go fish, z' end end end @@ -179,7 +179,7 @@ module Hyperclient end it 'proxies to the response object' do - resource._success?.must_equal true + _(resource._success?).must_equal true end end @@ -189,7 +189,7 @@ module Hyperclient end it 'returns nil' do - resource._success?.must_be_nil + _(resource._success?).must_be_nil end end end @@ -205,7 +205,7 @@ module Hyperclient end it 'proxies to the response object' do - resource._status.must_equal 200 + _(resource._status).must_equal 200 end end @@ -215,7 +215,7 @@ module Hyperclient end it 'returns nil' do - resource._status.must_be_nil + _(resource._status).must_be_nil end end end diff --git a/test/hyperclient_test.rb b/test/hyperclient_test.rb index c64dd33..9efd2a3 100644 --- a/test/hyperclient_test.rb +++ b/test/hyperclient_test.rb @@ -20,23 +20,22 @@ end it 'creates a Faraday connection with the default and additional headers' do - client.headers['Content-Type'].must_equal 'application/hal+json' - client.headers['Accept'].must_equal 'application/hal+json,application/json' - client.headers['Access-Token'].must_equal 'token' + _(client.headers['Content-Type']).must_equal 'application/hal+json' + _(client.headers['Accept']).must_equal 'application/hal+json,application/json' + _(client.headers['Access-Token']).must_equal 'token' end it 'creates a Faraday connection with the entry point url' do - client.connection.url_prefix.to_s.must_equal 'http://api.example.org/' + _(client.connection.url_prefix.to_s).must_equal 'http://api.example.org/' end it 'creates a Faraday connection with the default block plus any additional handlers' do handlers = client.connection.builder.handlers - handlers.must_include Faraday::Request::OAuth - handlers.must_include Faraday::Response::RaiseError - handlers.must_include FaradayMiddleware::FollowRedirects - handlers.must_include FaradayMiddleware::EncodeHalJson - handlers.must_include FaradayMiddleware::ParseHalJson - handlers.must_include Faraday::Adapter::NetHttp + _(handlers).must_include Faraday::Request::OAuth + _(handlers).must_include Faraday::Response::RaiseError + _(handlers).must_include FaradayMiddleware::FollowRedirects + _(handlers).must_include FaradayMiddleware::EncodeHalJson + _(handlers).must_include FaradayMiddleware::ParseHalJson end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 0041005..80bd6f7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,12 +2,16 @@ require 'minitest/autorun' require 'minitest/pride' -require 'mocha/setup' +require 'mocha/minitest' require 'json' MiniTest::Test.class_eval do def stub_request(conn, adapter_class = Faraday::Adapter::Test, &stubs_block) adapter_handler = conn.builder.handlers.find { |h| h.klass < Faraday::Adapter } - conn.builder.swap(adapter_handler, adapter_class, &stubs_block) + if adapter_handler + conn.builder.swap(adapter_handler, adapter_class, &stubs_block) + else + conn.builder.adapter adapter_class, &stubs_block + end end end