Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added real Net::HTTP option for integration tests #119

Open
wants to merge 1 commit into from

1 participant

@dlozano

Sometimes in integration tests you might want to do end-to-end tests and hit a external site.
http_mock.rb file requirement stubs all ActiveResource::Connections and at the same time all ActiveResource::Base classes to use a mock. From that point one you can't undo it without hacks.

This is an approach similar to other testing gems like webmock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2014
  1. @dlozano
This page is out of date. Refresh to see the latest.
Showing with 82 additions and 1 deletion.
  1. +44 −1 lib/active_resource/http_mock.rb
  2. +38 −0 test/cases/connection_test.rb
View
45 lib/active_resource/http_mock.rb
@@ -214,6 +214,31 @@ def reset!
requests.clear
responses.clear
end
+
+ # Enables all ActiveResource::Connection instances to use real
+ # Net::HTTP instance instead of a mock.
+ def enable_net_connection!
+ @@net_connection_enabled = true
+ end
+
+ # Sets all ActiveResource::Connection to use HttpMock instances.
+ def disable_net_connection!
+ @@net_connection_enabled = false
+ end
+
+ # Checks if real requests can be used instead of the default mock used in tests.
+ def net_connection_enabled?
+ if defined?(@@net_connection_enabled)
+ @@net_connection_enabled
+ else
+ @@net_connection_enabled = false
+ end
+ end
+
+ def net_connection_disabled?
+ !net_connection_enabled?
+ end
+
end
# body? methods
@@ -325,8 +350,26 @@ class Connection
private
silence_warnings do
def http
- @http ||= HttpMock.new(@site)
+ if unstub_http?
+ @http = configure_http(new_http)
+ elsif stub_http?
+ @http = http_stub
+ end
+ @http ||= http_stub
+ end
+
+ def http_stub
+ HttpMock.new(@site)
end
+
+ def unstub_http?
+ HttpMock.net_connection_enabled? && defined?(@http) && @http.kind_of?(HttpMock)
+ end
+
+ def stub_http?
+ HttpMock.net_connection_disabled? && defined?(@http) && @http.kind_of?(Net::HTTP)
+ end
+
end
end
end
View
38 test/cases/connection_test.rb
@@ -258,6 +258,44 @@ def test_auth_type_ignores_nonsensical_values
assert_equal(:basic, @conn.auth_type)
end
+ def test_disable_net_connection_by_default_when_http_mock_is_available
+ assert_equal(ActiveResource::HttpMock, @conn.send(:http).class)
+ end
+
+ def test_enable_net_connection
+ @conn.send(:http)
+ keep_net_connection_status do
+ ActiveResource::HttpMock.enable_net_connection!
+ assert @conn.send(:http).kind_of?(Net::HTTP)
+ end
+ end
+
+ def test_disable_net_connection
+ keep_net_connection_status do
+ ActiveResource::HttpMock.enable_net_connection!
+ @conn.send(:http)
+ ActiveResource::HttpMock.disable_net_connection!
+ assert @conn.send(:http).kind_of?(ActiveResource::HttpMock)
+ end
+ end
+
+ def keep_net_connection_status
+ old = ActiveResource::HttpMock.net_connection_enabled?
+ begin
+ yield
+ ensure
+ if old
+ ActiveResource::HttpMock.enable_net_connection!
+ else
+ ActiveResource::HttpMock.disable_net_connection!
+ end
+ end
+ end
+
+ def new_connection
+ ActiveResource::Connection.new('http://localhost')
+ end
+
protected
def assert_response_raises(klass, code)
assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
Something went wrong with that request. Please try again.