Skip to content

Commit

Permalink
Changed syntax from "should have_not" to correct "should_not have_"
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Blimke committed Nov 20, 2009
1 parent 7975a15 commit 7fb73f9
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -10,7 +10,7 @@ Features
* Matching requests based on method, url, headers and body
* Support for Test::Unit and RSpec (and can be easily extended to other frameworks)
* Support for Net::Http and other http libraries based on Net::Http
* Architecture allows adding other http library adapters easily
* Adding other http library adapters is easy


Installation
Expand Down Expand Up @@ -136,13 +136,13 @@ Now you are ready to write your tests/specs with stubbed HTTP calls.

request(:post, "www.something.com").should have_been_made.times(3)

request(:any, "www.example.com").should have_not_been_made
request(:any, "www.example.com").should_not have_been_made

### RSpec matchers 2 ([fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher) style)

WebMock.should have_requested(:get, "www.google.com").with(:body => "abc", :headers => {'Content-Length' => 3}).twice

WebMock.should have_not_requested(:get, "www.something.com")
WebMock.should_not have_requested(:get, "www.something.com")

Notes
-----
Expand Down
23 changes: 14 additions & 9 deletions lib/webmock/adapters/rspec/request_profile_matcher.rb
@@ -1,37 +1,42 @@
module WebMock
class RequestProfileMatcher

def initialize
@request_execution_verifier = RequestExecutionVerifier.new
end

def once
@request_execution_verifier.expected_times_executed = 1
self
end

def twice
@request_execution_verifier.expected_times_executed = 2
self
end

def times(times)
@request_execution_verifier.expected_times_executed = times.to_i
self
end

def matches?(request_profile)
@request_execution_verifier.request_profile = request_profile
@request_execution_verifier.verify
@request_execution_verifier.matches?
end


def does_not_match?(request_profile)
@request_execution_verifier.request_profile = request_profile
@request_execution_verifier.does_not_match?
end

def failure_message
@request_execution_verifier.failure_message
end


def negative_failure_message
@request_execution_verifier.negative_failure_message
end
end
end
end
6 changes: 5 additions & 1 deletion lib/webmock/adapters/rspec/webmock_matcher.rb
Expand Up @@ -30,7 +30,11 @@ def times(times)
end

def matches?(webmock)
@request_execution_verifier.verify
@request_execution_verifier.matches?
end

def does_not_match?(webmock)
@request_execution_verifier.does_not_match?
end

def failure_message
Expand Down
30 changes: 25 additions & 5 deletions lib/webmock/request_execution_verifier.rb
@@ -1,22 +1,42 @@
module WebMock
class RequestExecutionVerifier

attr_accessor :request_profile, :expected_times_executed, :times_executed

def initialize(request_profile = nil, expected_times_executed = nil)
@request_profile = request_profile
@expected_times_executed = expected_times_executed || 1
@expected_times_executed = expected_times_executed
end

def matches?
@times_executed =
RequestRegistry.instance.times_executed(@request_profile)
@times_executed == (@expected_times_executed || 1)
end

def verify
@times_executed =
def does_not_match?
@times_executed =
RequestRegistry.instance.times_executed(@request_profile)
@times_executed == @expected_times_executed
if @expected_times_executed
@times_executed != @expected_times_executed
else
@times_executed == 0
end
end


def failure_message
expected_times_executed = @expected_times_executed || 1
%Q(The request #{request_profile.to_s} was expected to execute #{expected_times_executed} time#{ (expected_times_executed == 1) ? '' : 's'} but it executed #{times_executed} time#{ (times_executed == 1) ? '' : 's'})
end

def negative_failure_message
if @expected_times_executed
%Q(The request #{request_profile.to_s} was not expected to execute #{expected_times_executed} time#{ (expected_times_executed == 1) ? '' : 's'} but it executed #{times_executed} time#{ (times_executed == 1) ? '' : 's'})
else
%Q(The request #{request_profile.to_s} was expected to execute 0 times but it executed #{times_executed} time#{ (times_executed == 1) ? '' : 's'})
end
end

end
end
6 changes: 4 additions & 2 deletions lib/webmock/webmock.rb
Expand Up @@ -18,11 +18,13 @@ def assert_requested(method, url, options = {})
expected_times_executed = options.delete(:times) || 1
request = RequestProfile.new(method, url, options[:body], options[:headers])
verifier = RequestExecutionVerifier.new(request, expected_times_executed)
assertion_failure(verifier.failure_message) unless verifier.verify
assertion_failure(verifier.failure_message) unless verifier.matches?
end

def assert_not_requested(method, url, options = {})
assert_requested(method, url, options.update(:times => 0))
request = RequestProfile.new(method, url, options[:body], options[:headers])
verifier = RequestExecutionVerifier.new(request, options.delete(:times))
assertion_failure(verifier.negative_failure_message) unless verifier.does_not_match?
end

def self.allow_net_connect!
Expand Down
53 changes: 46 additions & 7 deletions spec/request_execution_verifier_spec.rb
Expand Up @@ -23,29 +23,68 @@
end

end

describe "negative failure message" do

describe "verify" do
it "should report failure message if it executed number of times specified" do
@verifier.times_executed = 2
@verifier.expected_times_executed = 2
@verifier.negative_failure_message.should == "The request www.google.com was not expected to execute 2 times but it executed 2 times"
end

it "should report failure message when not expected request but it executed" do
@verifier.times_executed = 1
@verifier.negative_failure_message.should == "The request www.google.com was expected to execute 0 times but it executed 1 time"
end

end

describe "matches?" do

it "should succeed if request was executed expected number of times" do
RequestRegistry.instance.
should_receive(:times_executed).with(@request_profile).and_return(10)
@verifier.expected_times_executed = 10
@verifier.verify.should be_true
@verifier.matches?.should be_true
end

it "should fail if request was not executed expected number of times" do
RequestRegistry.instance.
should_receive(:times_executed).with(@request_profile).and_return(10)
@verifier.expected_times_executed = 5
@verifier.verify.should be_false
@verifier.matches?.should be_false
end

end

describe "does_not_match?" do

it "should fail if request executed expected number of times" do
RequestRegistry.instance.
should_receive(:times_executed).with(@request_profile).and_return(10)
@verifier.expected_times_executed = 10
@verifier.does_not_match?.should be_false
end

it "should succeed if request was not executed at all and expected number of times was not set" do
RequestRegistry.instance.
should_receive(:times_executed).with(@request_profile).and_return(0)
@verifier.does_not_match?.should be_true
end

it "should fail if request was executed and expected number of times was not set" do
RequestRegistry.instance.
should_receive(:times_executed).with(@request_profile).and_return(1)
@verifier.does_not_match?.should be_false
end

it "should succeed if request was not executed expected number of times" do
RequestRegistry.instance.
should_receive(:times_executed).with(@request_profile).and_return(10)
@verifier.expected_times_executed = 5
@verifier.does_not_match?.should be_true
end

def verify
@times_executed =
RequestRegistry.instance.times_executed(@request_profile)
@times_executed == @expected_times_executed
end

end
2 changes: 1 addition & 1 deletion spec/response_spec.rb
Expand Up @@ -55,5 +55,5 @@
end

end

end
10 changes: 6 additions & 4 deletions spec/webmock_spec.rb
Expand Up @@ -230,14 +230,14 @@ class MyException < StandardError; end;

it "should pass if request was not expected and not executed" do
lambda {
request(:get, "http://www.google.com").should have_not_been_made
request(:get, "http://www.google.com").should_not have_been_made
}.should_not raise_error
end

it "should fail if request was not expected but executed" do
lambda {
http_request(:get, "http://www.google.com/")
request(:get, "http://www.google.com").should have_not_been_made
request(:get, "http://www.google.com").should_not have_been_made
}.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
end

Expand Down Expand Up @@ -434,10 +434,12 @@ class MyException < StandardError; end;
it "should verify that non expected requests didn't occur" do
lambda {
http_request(:get, "http://www.google.com/")
WebMock.should have_not_requested(:get, "http://www.google.com")
WebMock.should_not have_requested(:get, "http://www.google.com")
}.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
end
end



describe "using assert_requested" do

Expand Down Expand Up @@ -482,7 +484,7 @@ class MyException < StandardError; end;
it "should verify that non expected requests didn't occur" do
lambda {
http_request(:get, "http://www.google.com/")
request(:get, "http://www.google.com").should have_not_been_made
request(:get, "http://www.google.com").should_not have_been_made
}.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
end
end
Expand Down

0 comments on commit 7fb73f9

Please sign in to comment.