Skip to content
Browse files

assert_requested and assert_not_requested can accept either method an…

…d uri, or RequestStub
  • Loading branch information...
1 parent 84ef7a7 commit ed795fa552a601d54fdc4c84a3ec4fda6c9c71a3 @bblimke committed Feb 4, 2012
Showing with 36 additions and 28 deletions.
  1. +2 −2 README.md
  2. +22 −14 lib/webmock/api.rb
  3. +4 −4 minitest/webmock_spec.rb
  4. +4 −4 spec/acceptance/shared/request_expectations.rb
  5. +4 −4 test/shared_test.rb
View
4 README.md
@@ -384,8 +384,8 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
Net::HTTP.get('www.example.com', '/')
- assert_stub_requested(stub_get)
- assert_stub_not_requested(stub_post)
+ assert_requested(stub_get)
+ assert_not_requested(stub_post)
### Setting expectations in RSpec on `WebMock` module
View
36 lib/webmock/api.rb
@@ -17,27 +17,35 @@ class << self
alias :request :a_request
end
- def assert_requested(method, uri, options = {}, &block)
- expected_times_executed = options.delete(:times) || 1
- request = WebMock::RequestPattern.new(method, uri, options).with(&block)
- verifier = WebMock::RequestExecutionVerifier.new(request, expected_times_executed)
- WebMock::AssertionFailure.failure(verifier.failure_message) unless verifier.matches?
+
+ def assert_requested(*args, &block)
+ if not args[0].is_a?(WebMock::RequestStub)
+ args = convert_uri_method_and_options_to_request_and_options(*args, &block)
+ end
+ assert_request_requested(*args, &block)
end
- def assert_not_requested(method, uri, options = {}, &block)
- request = WebMock::RequestPattern.new(method, uri, options).with(&block)
- verifier = WebMock::RequestExecutionVerifier.new(request, options.delete(:times))
- WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
+ def assert_not_requested(*args, &block)
+ if not args[0].is_a?(WebMock::RequestStub)
+ args = convert_uri_method_and_options_to_request_and_options(*args, &block)
+ end
+ assert_request_not_requested(*args, &block)
end
- def assert_stub_requested(stub, options = {})
- expected_times_executed = options.delete(:times) || 1
- verifier = WebMock::RequestExecutionVerifier.new(stub, expected_times_executed)
+ private
+
+ def convert_uri_method_and_options_to_request_and_options(*args, &block)
+ request = WebMock::RequestPattern.new(*args).with(&block)
+ [request, args[2] || {}]
+ end
+
+ def assert_request_requested(request, options = {})
+ verifier = WebMock::RequestExecutionVerifier.new(request, options.delete(:times) || 1)
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))
+ def assert_request_not_requested(request, options = {})
+ verifier = WebMock::RequestExecutionVerifier.new(request, options.delete(:times))
WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
end
View
8 minitest/webmock_spec.rb
@@ -20,14 +20,14 @@
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)
+ assert_requested(@stub_http, :times => 1)
+ assert_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)
+ assert_requested(@stub_https, :times => 2)
end
it "should verify that expect request didn't occur" do
@@ -42,7 +42,7 @@
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)
+ assert_requested(@stub_http)
end
end
View
8 spec/acceptance/shared/request_expectations.rb
@@ -542,21 +542,21 @@
end
end
- describe "when expectation is declared using assert_stub_requested" do
+ describe "when expectation is declared using assert_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)
+ assert_requested(stub_http, :times => 1)
+ assert_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)
+ assert_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
View
8 test/shared_test.rb
@@ -24,8 +24,8 @@ def test_verification_that_expected_request_occured
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)
+ assert_requested(@stub_http, :times => 1)
+ assert_requested(@stub_http)
end
def test_verification_that_expected_request_didnt_occur
@@ -40,7 +40,7 @@ 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)
+ assert_requested(@stub_http)
end
end
@@ -63,7 +63,7 @@ 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)
+ assert_not_requested(@stub_http)
end
end
end

0 comments on commit ed795fa

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