Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added minitest spec to ensure webmock works with Minitest::Spec. Unfo…

…rtunately there is no easy way to add Minitest matchers.
  • Loading branch information...
commit b457d3898fa6408f28d01ef79b32078d852c3285 1 parent c6ef444
@bblimke authored
View
24 minitest/test_helper.rb
@@ -3,6 +3,8 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require File.expand_path(File.dirname(__FILE__) + '/../test/http_request')
+
gem "minitest"
require 'minitest/autorun'
require 'webmock/minitest'
@@ -11,13 +13,17 @@ class MiniTest::Unit::TestCase
def assert_raise(*exp, &block)
assert_raises(*exp, &block)
end
- AssertionFailedError = MiniTest::Assertion
- def assert_fail(message, &block)
- e = assert_raises(AssertionFailedError, &block)
- if message.is_a?(Regexp)
- assert_match(message, e.message)
- else
- assert_equal(message, e.message)
- end
- end
+
+ def assert_raise_with_message(e, message, &block)
+ e = assert_raises(e, &block)
+ if message.is_a?(Regexp)
+ assert_match(message, e.message)
+ else
+ assert_equal(message, e.message)
+ end
+ end
+
+ def assert_fail(message, &block)
+ assert_raise_with_message(MiniTest::Assertion, message, &block)
+ end
end
View
30 minitest/webmock_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+
+ describe "Webmock" do
+ include HttpRequestTestHelper
+
+ before do
+ stub_http_request(:any, "http://www.example.com")
+ stub_http_request(:any, "https://www.example.com")
+ end
+
+ it "should raise error on non stubbed request" do
+ lambda { http_request(:get, "http://www.example.net/") }.must_raise(WebMock::NetConnectNotAllowedError)
+ end
+
+ it "should verify that expected request occured" do
+ http_request(:get, "http://www.example.com/")
+ assert_requested(:get, "http://www.example.com", :times => 1)
+ assert_requested(:get, "http://www.example.com")
+ end
+
+ it "should verify that expect request didn't occur" do
+ expected_message = "The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times"
+ expected_message << "\n\nThe following requests were made:\n\nNo requests were made.\n============================================================"
+ assert_fail(expected_message) do
+ assert_requested(:get, "http://www.example.com")
+ end
+ end
+
+ end
+
View
24 test/http_request.rb
@@ -0,0 +1,24 @@
+require 'ostruct'
+
+module HttpRequestTestHelper
+ def http_request(method, uri, options = {})
+ begin
+ uri = URI.parse(uri)
+ rescue
+ uri = Addressable::URI.heuristic_parse(uri)
+ end
+ response = nil
+ clazz = ::Net::HTTP.const_get("#{method.to_s.capitalize}")
+ req = clazz.new("#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", options[:headers])
+ req.basic_auth uri.user, uri.password if uri.user
+ http = ::Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true if uri.scheme == "https"
+ response = http.start {|http|
+ http.request(req, options[:body])
+ }
+ OpenStruct.new({
+ :body => response.body,
+ :headers => response,
+ :status => response.code })
+ end
+end
View
26 test/shared_test.rb
@@ -1,27 +1,7 @@
-require 'ostruct'
+require File.expand_path(File.dirname(__FILE__) + '/http_request')
module SharedTest
- def http_request(method, uri, options = {})
- begin
- uri = URI.parse(uri)
- rescue
- uri = Addressable::URI.heuristic_parse(uri)
- end
- response = nil
- clazz = ::Net::HTTP.const_get("#{method.to_s.capitalize}")
- req = clazz.new("#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", options[:headers])
- req.basic_auth uri.user, uri.password if uri.user
- http = ::Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = true if uri.scheme == "https"
- response = http.start {|http|
- http.request(req, options[:body])
- }
- OpenStruct.new({
- :body => response.body,
- :headers => response,
- :status => response.code })
- end
-
+ include HttpRequestTestHelper
def setup
super
@@ -31,7 +11,7 @@ def setup
def test_error_on_non_stubbed_request
default_ruby_headers = (RUBY_VERSION >= "1.9.1") ? "{'Accept'=>'*/*', 'User-Agent'=>'Ruby'}" : "{'Accept'=>'*/*'}"
- assert_raise(WebMock::NetConnectNotAllowedError, "Real HTTP connections are disabled. Unregistered request: GET http://www.example.net/ with headers #{default_ruby_headers}") do
+ assert_raise_with_message(WebMock::NetConnectNotAllowedError, "Real HTTP connections are disabled. Unregistered request: GET http://www.example.net/ with headers #{default_ruby_headers}\n\nYou can stub this request with the following snippet:\n\nstub_request(:get, \"http://www.example.net/\").\n with(:headers => {'Accept'=>'*/*'}).\n to_return(:status => 200, :body => \"\", :headers => {})\n\n============================================================") do
http_request(:get, "http://www.example.net/")
end
end
View
8 test/test_helper.rb
@@ -8,12 +8,16 @@
class Test::Unit::TestCase
AssertionFailedError = Test::Unit::AssertionFailedError rescue MiniTest::Assertion
- def assert_fail(message, &block)
- e = assert_raise(AssertionFailedError, &block)
+ def assert_raise_with_message(e, message, &block)
+ e = assert_raise(e, &block)
if message.is_a?(Regexp)
assert_match(message, e.message)
else
assert_equal(message, e.message)
end
end
+
+ def assert_fail(message, &block)
+ assert_raise_with_message(AssertionFailedError, message, &block)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.