Skip to content
This repository
Browse code

MiniTest support.

  • Loading branch information...
commit fdeeae9c2a128071cf32c71d5b6dfdf66ab9aef8 1 parent 03bda1c
pete higgins phiggins authored

Showing 2 changed files with 60 additions and 0 deletions. Show diff stats Hide diff stats

  1. +15 0 lib/webmock/minitest.rb
  2. +45 0 spec/minitest_spec.rb
15 lib/webmock/minitest.rb
... ... @@ -0,0 +1,15 @@
  1 +require 'minitest/unit'
  2 +require 'webmock'
  3 +
  4 +MiniTest::Unit::TestCase.class_eval do
  5 + include WebMock::API
  6 +
  7 + alias_method :teardown_without_webmock, :teardown
  8 + def teardown_with_webmock
  9 + teardown_without_webmock
  10 + WebMock.reset!
  11 + end
  12 + alias_method :teardown, :teardown_with_webmock
  13 +end
  14 +
  15 +WebMock::AssertionFailure.error_class = MiniTest::Assertion
45 spec/minitest_spec.rb
... ... @@ -0,0 +1,45 @@
  1 +require 'spec_helper'
  2 +
  3 +SCRIPT = <<-CODE
  4 +require "minitest/autorun"
  5 +require "webmock/minitest"
  6 +
  7 +class MiniTestWebMockTest < MiniTest::Unit::TestCase
  8 + def test_that_passes
  9 + stub_request(:any, "http://example.com")
  10 +
  11 + r = Net::HTTP.get_response(URI("http://example.com"))
  12 +
  13 + assert_equal 200, r.code.to_i
  14 + end
  15 +
  16 + def test_that_fails
  17 + r = Net::HTTP.get_response(URI("http://example.com"))
  18 +
  19 + assert_equal 200, r.code.to_i
  20 + end
  21 +end
  22 +CODE
  23 +
  24 +describe "MiniTest and WebMock" do
  25 + def run_test
  26 + # borrowed from: http://stackoverflow.com/questions/213368
  27 + ruby = File.join(Config::CONFIG.values_at('bindir', 'ruby_install_name')).
  28 + sub(/.*\s.*/m, '"\&"')
  29 +
  30 + # This value makes it so the tests run in the order defined.
  31 + seed = 9001
  32 +
  33 + `#{ruby} -Ilib -e '#{SCRIPT}' -- --verbose --seed #{seed}`
  34 + end
  35 +
  36 + it "has access to WebMock::API's methods" do
  37 + re = /^MiniTestWebMockTest#test_that_passes: [0-9]\.[0-9]+ s: \.$/
  38 + run_test.should match re
  39 + end
  40 +
  41 + it "clears WebMock's registry between tests" do
  42 + re = /test_that_fails\(MiniTestWebMockTest\):\nWebMock::NetConnectNotAllowedError:/m
  43 + run_test.should match re
  44 + end
  45 +end

0 comments on commit fdeeae9

Please sign in to comment.
Something went wrong with that request. Please try again.