Skip to content

Commit

Permalink
The expectation failure message contains a list of made requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bblimke committed Nov 11, 2010
1 parent b49cae2 commit 3ea53ef
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
12 changes: 10 additions & 2 deletions lib/webmock/request_execution_verifier.rb
Expand Up @@ -27,21 +27,29 @@ def does_not_match?

def failure_message
expected_times_executed = @expected_times_executed || 1
%Q(The request #{request_pattern.to_s} was expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
text = %Q(The request #{request_pattern.to_s} was expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
text << executed_requests
text
end

def negative_failure_message
if @expected_times_executed
text = if @expected_times_executed
%Q(The request #{request_pattern.to_s} was not expected to execute #{times(expected_times_executed)} but it executed #{times(times_executed)})
else
%Q(The request #{request_pattern.to_s} was expected to execute 0 times but it executed #{times(times_executed)})
end
text << executed_requests
text
end

private

def times(times)
"#{times} time#{ (times == 1) ? '' : 's'}"
end

def executed_requests
"\n\nThe following requests were made:\n\n#{RequestRegistry.instance.to_s}\n" + "="*60
end
end
end
18 changes: 14 additions & 4 deletions spec/request_execution_verifier_spec.rb
Expand Up @@ -5,6 +5,8 @@
@verifier = WebMock::RequestExecutionVerifier.new
@request_pattern = mock(WebMock::RequestPattern, :to_s => "www.example.com")
@verifier.request_pattern = @request_pattern
WebMock::RequestRegistry.instance.stub(:to_s).and_return("executed requests")
@executed_requests_info = "\n\nThe following requests were made:\n\nexecuted requests\n" + "="*60
end


Expand All @@ -13,13 +15,17 @@
it "should report failure message" do
@verifier.times_executed = 0
@verifier.expected_times_executed = 2
@verifier.failure_message.should == "The request www.example.com was expected to execute 2 times but it executed 0 times"
expected_text = "The request www.example.com was expected to execute 2 times but it executed 0 times"
expected_text << @executed_requests_info
@verifier.failure_message.should == expected_text
end

it "should report failure message correctly when executed times is one" do
@verifier.times_executed = 1
@verifier.expected_times_executed = 1
@verifier.failure_message.should == "The request www.example.com was expected to execute 1 time but it executed 1 time"
expected_text = "The request www.example.com was expected to execute 1 time but it executed 1 time"
expected_text << @executed_requests_info
@verifier.failure_message.should == expected_text
end

end
Expand All @@ -29,12 +35,16 @@
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.example.com was not expected to execute 2 times but it executed 2 times"
expected_text = "The request www.example.com was not expected to execute 2 times but it executed 2 times"
expected_text << @executed_requests_info
@verifier.negative_failure_message.should == expected_text
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.example.com was expected to execute 0 times but it executed 1 time"
expected_text = "The request www.example.com was expected to execute 0 times but it executed 1 time"
expected_text << @executed_requests_info
@verifier.negative_failure_message.should == expected_text
end

end
Expand Down

0 comments on commit 3ea53ef

Please sign in to comment.