Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix `should be =~` matcher.

It looks like it never actually worked since the be matcher
didn't define a `=~` operator previously.

Fixes #199.
  • Loading branch information...
commit 5d1473a4542ddb4855ec9aae027af00636fedfdf 1 parent f12659e
@myronmarston myronmarston authored
View
4 Changelog.md
@@ -5,6 +5,10 @@ Bug fixes
* Fix `change` matcher so that it dups strings in order to handle
mutated strings (Myron Marston).
+* Fix `should be =~ /some regex/` / `expect(...).to be =~ /some regex/`.
+ Previously, these either failed with a confusing `undefined method
+ matches?' for false:FalseClass` error or were no-ops that didn't
+ actually verify anything (Myron Marston).
Enhancements
View
2  lib/rspec/matchers/built_in/be.rb
@@ -62,7 +62,7 @@ def failure_message_for_should_not
"expected #{@actual.inspect} to evaluate to false"
end
- [:==, :<, :<=, :>=, :>, :===].each do |operator|
+ [:==, :<, :<=, :>=, :>, :===, :=~].each do |operator|
define_method operator do |operand|
BeComparedTo.new(operand, operator)
end
View
24 spec/rspec/matchers/be_spec.rb
@@ -347,6 +347,30 @@
end
end
+describe "expect(...).to be =~" do
+ it "passes when =~ operator returns true" do
+ expect("a string").to be =~ /str/
+ end
+
+ it "fails when =~ operator returns false" do
+ expect {
+ expect("a string").to be =~ /blah/
+ }.to fail_with(%Q|expected: =~ /blah/\n got: "a string"|)
+ end
+end
+
+describe "should be =~", :uses_should do
+ it "passes when =~ operator returns true" do
+ "a string".should be =~ /str/
+ end
+
+ it "fails when =~ operator returns false" do
+ expect {
+ "a string".should be =~ /blah/
+ }.to fail_with(%Q|expected: =~ /blah/\n got: "a string"|)
+ end
+end
+
describe "expect(...).to be ===" do
it "passes when === operator returns true" do
expect(Hash).to be === Hash.new
Please sign in to comment.
Something went wrong with that request. Please try again.