Permalink
Browse files

Added Test::Unit methods +assert_stub_requested+ and +assert_stub_not…

…_requested+
  • Loading branch information...
1 parent 2675b10 commit 84ef7a7edea58e8524b556c3bef31f639f5fca96 @nfo nfo committed with Sep 7, 2011
Showing with 87 additions and 4 deletions.
  1. +10 −0 README.md
  2. +12 −0 lib/webmock/api.rb
  3. +22 −2 minitest/webmock_spec.rb
  4. +19 −0 spec/acceptance/shared/request_expectations.rb
  5. +24 −2 test/shared_test.rb
View
@@ -377,6 +377,16 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
assert_requested :get, "http://www.example.com" # ===> Success
+### Setting expectations in Test::Unit on the stub
+
+ stub_get = stub_request(:get, "www.example.com")
+ stub_post = stub_request(:post, "www.example.com")
+
+ Net::HTTP.get('www.example.com', '/')
+
+ assert_stub_requested(stub_get)
+ assert_stub_not_requested(stub_post)
+
### Setting expectations in RSpec on `WebMock` module
This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
View
@@ -29,5 +29,17 @@ def assert_not_requested(method, uri, options = {}, &block)
verifier = WebMock::RequestExecutionVerifier.new(request, options.delete(:times))
WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
end
+
+ def assert_stub_requested(stub, options = {})
+ expected_times_executed = options.delete(:times) || 1
+ verifier = WebMock::RequestExecutionVerifier.new(stub, expected_times_executed)
+ WebMock::AssertionFailure.failure(verifier.failure_message) unless verifier.matches?
+ end
+
+ def assert_stub_not_requested(stub, options = {})
+ verifier = WebMock::RequestExecutionVerifier.new(stub, options.delete(:times))
+ WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
+ end
+
end
end
View
@@ -4,8 +4,8 @@
include HttpRequestTestHelper
before do
- stub_http_request(:any, "http://www.example.com")
- stub_http_request(:any, "https://www.example.com")
+ @stub_http = stub_http_request(:any, "http://www.example.com")
+ @stub_https = stub_http_request(:any, "https://www.example.com")
end
it "should raise error on non stubbed request" do
@@ -18,6 +18,18 @@
assert_requested(:get, "http://www.example.com")
end
+ it "should verify that expected http stub occured" do
+ http_request(:get, "http://www.example.com/")
+ assert_stub_requested(@stub_http, :times => 1)
+ assert_stub_requested(@stub_http)
+ end
+
+ it "should verify that expected https stub occured" do
+ http_request(:get, "https://www.example.com/")
+ http_request(:get, "https://www.example.com/")
+ assert_stub_requested(@stub_https, :times => 2)
+ 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============================================================"
@@ -26,5 +38,13 @@
end
end
+ it "should verify that expect stub didn't occur" do
+ expected_message = "The request ANY 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_stub_requested(@stub_http)
+ end
+ end
+
end
@@ -541,6 +541,25 @@
}.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
end
end
+
+ describe "when expectation is declared using assert_stub_requested" do
+ it "should satisfy expectation if requests was made" do
+ stub_http = stub_http_request(:get, "http://www.example.com")
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ assert_stub_requested(stub_http, :times => 1)
+ assert_stub_requested(stub_http)
+ }.should_not raise_error
+ end
+
+ it "should fail if request expected not to be made was not wade" do
+ stub_http = stub_http_request(:get, "http://www.example.com")
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ assert_stub_not_requested(stub_http)
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
+ end
+ end
end
View
@@ -5,8 +5,8 @@ module SharedTest
def setup
super
- stub_http_request(:any, "http://www.example.com")
- stub_http_request(:any, "https://www.example.com")
+ @stub_http = stub_http_request(:any, "http://www.example.com")
+ @stub_https = stub_http_request(:any, "https://www.example.com")
end
def test_error_on_non_stubbed_request
@@ -22,6 +22,12 @@ def test_verification_that_expected_request_occured
assert_requested(:get, "http://www.example.com")
end
+ def test_verification_that_expected_stub_occured
+ http_request(:get, "http://www.example.com/")
+ assert_stub_requested(@stub_http, :times => 1)
+ assert_stub_requested(@stub_http)
+ 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============================================================"
@@ -30,6 +36,14 @@ def test_verification_that_expected_request_didnt_occur
end
end
+ def test_verification_that_expected_request_didnt_occur
+ expected_message = "The request ANY 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_stub_requested(@stub_http)
+ 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'})
@@ -44,4 +58,12 @@ def test_verification_that_non_expected_request_didnt_occur
assert_not_requested(:get, "http://www.example.com")
end
end
+
+ def test_verification_that_non_expected_stub_didnt_occur
+ expected_message = %r(The request ANY 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_stub_not_requested(@stub_http)
+ end
+ end
end

0 comments on commit 84ef7a7

Please sign in to comment.