Permalink
Browse files

Created separate folder with minitest tests instead of running them f…

…rom within RSpec.
  • Loading branch information...
1 parent 06456a0 commit 4710449f0096c20decb51f2cf35284053789fbca @bblimke committed Jul 30, 2011
Showing with 105 additions and 116 deletions.
  1. +7 −1 Rakefile
  2. +23 −0 minitest/test_helper.rb
  3. +6 −0 minitest/test_webmock.rb
  4. +0 −47 spec/minitest_spec.rb
  5. +67 −0 test/shared_test.rb
  6. +0 −1 test/test_helper.rb
  7. +2 −67 test/test_webmock.rb
View
@@ -27,7 +27,13 @@ Rake::TestTask.new(:test) do |test|
test.warning = false
end
-task :default => [:spec, :test]
+Rake::TestTask.new(:minitest) do |test|
+ test.test_files = FileList["minitest/**/*.rb"].exclude("test/test_helper.rb")
+ test.verbose = false
+ test.warning = false
+end
+
+task :default => [:spec, :test, :minitest]
require 'rdoc/task'
RDoc::Task.new do |rdoc|
@@ -0,0 +1,23 @@
+require 'rubygems'
+
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+gem "minitest"
+require 'minitest/autorun'
+require 'webmock/minitest'
+
+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
+end
@@ -0,0 +1,6 @@
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../test/shared_test')
+
+class MiniTestWebMock < MiniTest::Unit::TestCase
+ include SharedTest
+end
View
@@ -1,47 +0,0 @@
-require 'spec_helper'
-
-SCRIPT = <<-CODE
-require "rubygems"
-gem "minitest"
-require "minitest/autorun"
-require "webmock/minitest"
-
-class MiniTestWebMockTest < MiniTest::Unit::TestCase
- def test_that_passes
- stub_request(:any, "http://example.com")
-
- r = Net::HTTP.get_response(URI("http://example.com"))
-
- assert_equal 200, r.code.to_i
- end
-
- def test_that_fails
- r = Net::HTTP.get_response(URI("http://example.com"))
-
- assert_equal 200, r.code.to_i
- end
-end
-CODE
-
-describe "MiniTest and WebMock" do
- def run_test
- # borrowed from: http://stackoverflow.com/questions/213368
- ruby = File.join(Config::CONFIG.values_at('bindir', 'ruby_install_name')).
- sub(/.*\s.*/m, '"\&"')
-
- # This value makes it so the tests run in the order defined.
- seed = 9001
-
- `#{ruby} -Ilib -e '#{SCRIPT}' -- --verbose --seed #{seed}`
- end
-
- it "has access to WebMock::API's methods" do
- re = /^MiniTestWebMockTest#test_that_passes = [0-9]\.[0-9]+ s = \.$/
- run_test.should match re
- end
-
- it "clears WebMock's registry between tests" do
- re = /test_that_fails\(MiniTestWebMockTest\):\nWebMock::NetConnectNotAllowedError:/m
- run_test.should match re
- end
-end
View
@@ -0,0 +1,67 @@
+require 'ostruct'
+
+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
+
+
+ def setup
+ super
+ stub_http_request(:any, "http://www.example.com")
+ stub_http_request(:any, "https://www.example.com")
+ end
+
+ 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
+ http_request(:get, "http://www.example.net/")
+ end
+ end
+
+ def test_verification_that_expected_request_occured
+ http_request(:get, "http://www.example.com/")
+ assert_requested(:get, "http://www.example.com", :times => 1)
+ assert_requested(:get, "http://www.example.com")
+ end
+
+ def test_verification_that_expected_request_didnt_occur
+ 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
+
+ def test_verification_that_expected_request_occured_with_body_and_headers
+ http_request(:get, "http://www.example.com/",
+ :body => "abc", :headers => {'A' => 'a'})
+ assert_requested(:get, "http://www.example.com",
+ :body => "abc", :headers => {'A' => 'a'})
+ end
+
+ def test_verification_that_non_expected_request_didnt_occur
+ expected_message = %r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time\n\nThe following requests were made:\n\nGET http://www.example.com/ with headers .+ was made 1 time\n\n============================================================)
+ assert_fail(expected_message) do
+ http_request(:get, "http://www.example.com/")
+ assert_not_requested(:get, "http://www.example.com")
+ end
+ end
+end
View
@@ -7,7 +7,6 @@
require 'test/unit'
class Test::Unit::TestCase
- include WebMock::API
AssertionFailedError = Test::Unit::AssertionFailedError rescue MiniTest::Assertion
def assert_fail(message, &block)
e = assert_raise(AssertionFailedError, &block)
View
@@ -1,71 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
-
-require 'ostruct'
+require File.expand_path(File.dirname(__FILE__) + '/shared_test')
class TestWebMock < Test::Unit::TestCase
-
- 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
-
-
- def setup
- super
- stub_http_request(:any, "http://www.example.com")
- stub_http_request(:any, "https://www.example.com")
- end
-
- 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
- http_request(:get, "http://www.example.net/")
- end
- end
-
- def test_verification_that_expected_request_occured
- http_request(:get, "http://www.example.com/")
- assert_requested(:get, "http://www.example.com", :times => 1)
- assert_requested(:get, "http://www.example.com")
- end
-
- def test_verification_that_expected_request_didnt_occur
- 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
-
- def test_verification_that_expected_request_occured_with_body_and_headers
- http_request(:get, "http://www.example.com/",
- :body => "abc", :headers => {'A' => 'a'})
- assert_requested(:get, "http://www.example.com",
- :body => "abc", :headers => {'A' => 'a'})
- end
-
- def test_verification_that_non_expected_request_didnt_occur
- expected_message = %r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time\n\nThe following requests were made:\n\nGET http://www.example.com/ with headers .+ was made 1 time\n\n============================================================)
- assert_fail(expected_message) do
- http_request(:get, "http://www.example.com/")
- assert_not_requested(:get, "http://www.example.com")
- end
- end
-
+ include SharedTest
end

0 comments on commit 4710449

Please sign in to comment.