Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MiniTest support.

  • Loading branch information...
commit fdeeae9c2a128071cf32c71d5b6dfdf66ab9aef8 1 parent 03bda1c
@phiggins phiggins authored
Showing with 60 additions and 0 deletions.
  1. +15 −0 lib/webmock/minitest.rb
  2. +45 −0 spec/minitest_spec.rb
View
15 lib/webmock/minitest.rb
@@ -0,0 +1,15 @@
+require 'minitest/unit'
+require 'webmock'
+
+MiniTest::Unit::TestCase.class_eval do
+ include WebMock::API
+
+ alias_method :teardown_without_webmock, :teardown
+ def teardown_with_webmock
+ teardown_without_webmock
+ WebMock.reset!
+ end
+ alias_method :teardown, :teardown_with_webmock
+end
+
+WebMock::AssertionFailure.error_class = MiniTest::Assertion
View
45 spec/minitest_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+SCRIPT = <<-CODE
+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
Please sign in to comment.
Something went wrong with that request. Please try again.