Permalink
Browse files

Merge remote branch 'afeld/expectations_on_stubs'

  • Loading branch information...
2 parents 3db1628 + 76c01cf commit 68ae37fc4aca8391dce725575e14535637430a1a @bblimke committed Jul 30, 2011
Showing with 69 additions and 5 deletions.
  1. +8 −2 README.md
  2. +7 −0 lib/webmock/request_stub.rb
  3. +6 −1 lib/webmock/rspec/matchers.rb
  4. +48 −2 spec/webmock_shared.rb
View
@@ -351,7 +351,7 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
assert_requested :get, "http://www.example.com" # ===> Success
-### Setting expectations in RSpec
+### Setting expectations in RSpec on `WebMock` module
This style is borrowed from [fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher)
require 'webmock/rspec'
@@ -367,7 +367,7 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
WebMock.should have_requested(:get, "www.example.com").
with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'})
-### Different way of setting expectations in RSpec
+### Setting expectations in RSpec with `a_request`
a_request(:post, "www.example.com").with(:body => "abc", :headers => {'Content-Length' => 3}).should have_been_made.once
@@ -382,6 +382,12 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
a_request(:post, "www.example.com").
with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'}).should have_been_made
+### Setting expectations in RSpec on the stub
+
+ stub = stub_request(:get, "www.example.com")
+ # ... make requests ...
+ stub.should have_been_requested
+
## Clearing stubs and request history
If you want to reset all current stubs and history of requests use `WebMock.reset!`
@@ -60,5 +60,12 @@ def times(number)
self
end
+ def matches?(request_signature)
+ self.request_pattern.matches?(request_signature)
+ end
+
+ def to_s
+ self.request_pattern.to_s
+ end
end
end
@@ -7,7 +7,12 @@ module Matchers
def have_been_made
WebMock::RequestPatternMatcher.new
end
-
+
+ def have_been_requested
+ WebMock::RequestPatternMatcher.new
+ end
+
+
def have_not_been_made
WebMock::RequestPatternMatcher.new.times(0)
end
View
@@ -1405,8 +1405,54 @@ def call(request)
end
end
end
-
-
+
+
+ describe "using matchers on the RequestStub" do
+
+ it "should verify expected requests occured" do
+ stub = stub_request(:get, "http://www.example.com/")
+ http_request(:get, "http://www.example.com/")
+ stub.should have_been_requested.once
+ end
+
+ it "should verify subsequent requests" do
+ stub = stub_request(:get, "http://www.example.com/")
+ http_request(:get, "http://www.example.com/")
+ stub.should have_been_requested.once
+ http_request(:get, "http://www.example.com/")
+ stub.should have_been_requested.twice
+ end
+
+ it "should verify expected requests occured" do
+ stub = stub_request(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'})
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
+ stub.should have_been_requested.once
+ end
+
+ it "should verify that non expected requests didn't occur" do
+ lambda {
+ stub = stub_request(:get, "http://www.example.com")
+ http_request(:get, "http://www.example.com/")
+ stub.should_not have_been_requested
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
+ end
+
+ it "should verify if non expected request executed and block evaluated to true" do
+ lambda {
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
+ http_request(:post, "http://www.example.com/", :body => "wadus")
+ stub.should_not have_been_requested
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
+ end
+
+ it "should verify if request was executed and block evaluated to true" do
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
+ http_request(:post, "http://www.example.com/", :body => "wadus")
+ stub.should have_been_requested
+ end
+ end
+
+
describe "when net connect allowed", :net_connect => true do
before(:each) do
WebMock.allow_net_connect!

0 comments on commit 68ae37f

Please sign in to comment.