Permalink
Browse files

allow expectations to be set on the stub itself

  • Loading branch information...
1 parent a251cb3 commit a6e08005c5c76a8721a07047bea90fb7f13c8bf3 @afeld afeld committed Jun 6, 2011
Showing with 68 additions and 5 deletions.
  1. +8 −2 README.md
  2. +4 −0 lib/webmock/request_stub.rb
  3. +6 −1 lib/webmock/rspec/matchers.rb
  4. +50 −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,9 @@ def times(number)
self
end
+ def matches?(request_signature)
+ # TODO fix failure message
+ self.request_pattern.matches?(request_signature)
+ 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
@@ -1374,8 +1374,56 @@ 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 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
+
+ it "should verify if request was executed and block evaluated to false" do
+ pending "fails with NetConnectNotAllowedError, instead of expected execution error"
+
+ lambda {
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
+ http_request(:post, "http://www.example.com/", :body => "abc")
+ stub.should have_been_requested
+ }.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 net connect allowed", :net_connect => true do
before(:each) do
WebMock.allow_net_connect!

0 comments on commit a6e0800

Please sign in to comment.