Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Explain assert_sent_to and assert_not_sent_to test failures

  • Loading branch information...
commit b143a62cf8b4ca3e2f35f43e20f8720794cce5ec 1 parent 452091d
@raws raws authored
Showing with 21 additions and 2 deletions.
  1. +21 −2 test/lib/hector/integration_test.rb
View
23 test/lib/hector/integration_test.rb
@@ -64,12 +64,31 @@ def capture_sent_data(connection)
def assert_sent_to(connection, line, &block)
sent_data = block ? capture_sent_data(connection, &block) : connection.sent_data
- assert sent_data =~ /^#{line.is_a?(Regexp) ? line : Regexp.escape(line)}/
+ assert sent_data =~ /^#{line.is_a?(Regexp) ? line : Regexp.escape(line)}/, explain_sent_to(line, sent_data)
+ end
+
+ def explain_sent_to(line, sent_data)
+ [].tap do |lines|
+ lines.push("Expected to receive #{line.inspect}, but did not receive it:")
+ lines.concat(sent_data.split(/[\r\n]+/).map { |line| line.inspect })
+ end.join("\n")
end
def assert_not_sent_to(connection, line, &block)
sent_data = block ? capture_sent_data(connection, &block) : connection.sent_data
- assert sent_data !~ /^#{line.is_a?(Regexp) ? line : Regexp.escape(line)}/
+ assert sent_data !~ /^#{line.is_a?(Regexp) ? line : Regexp.escape(line)}/, explain_not_sent_to(line, sent_data)
+ end
+
+ def explain_not_sent_to(line, sent_data)
+ sent_data = sent_data.split(/[\r\n]+/).map do |sent_line|
+ sent_line !~ /^#{line.is_a?(Regexp) ? line : Regexp.escape(line)}/ ? " #{sent_line.inspect}" : "* #{sent_line.inspect}"
+ end
+ line_number = sent_data.index { |line| line[0, 2] == "* " }
+
+ [].tap do |lines|
+ lines.push("Expected not to receive #{line.inspect}, but received it on line #{line_number}:")
+ lines.concat(sent_data)
+ end.join("\n")
end
def assert_nothing_sent_to(connection, &block)
Please sign in to comment.
Something went wrong with that request. Please try again.