包含待测对象的文件:foo.rb
class Foo
def a_string
"abcdef8"
end
end
规范文件:foo_spec.rb
require_relative "./foo"
describe Foo do
let(:foo) {Foo.new}
let(:my_matcher) {/^[a-z]+(\d)$/}
# This test passes
it "should match and pass" do
my_str = foo.a_string
my_matcher # <--- why does this affect the test?
matcher = my_str.match(my_matcher)
8.should == matcher[1].to_i
end
# This test fails
it "should also match and pass but fails" do
my_str = foo.a_string
#my_matcher #<---- the only change between the tests
matcher = my_str.match(my_matcher) #<---- when called here, it behaves differently
8.should == matcher[1].to_i
end
end
rspec foo_spec.rb
.F
Failures:
1) Foo should also match and pass but fails
Failure/Error: 8.should == matcher[1].to_i
NoMethodError:
undefined method `[]' for /^[a-z]+(\d)$/:Regexp
# ./foo_spec.rb:18:in `block (2 levels) in <top (required)>'
Finished in 0.00095 seconds
2 examples, 1 failure
Failed examples:
rspec ./foo_spec.rb:14 # Foo should also match and pass but fails
两个测试的唯一区别是是否调用了my_matcher
。我在检查my_matcher(即p my_matcher
)时首先注意到了这个问题,但只是在调用my_matcher
时也会出现这个问题。
这是一个错误,还是我做错了什么?也许它与捕获Regex数据有关?
这似乎非常奇怪,特别是对于红宝石。
为了它的价值,它是一个简单的(如果稍微减少DRY)修复。如果在my_matcher
块中声明it
,则按预期工作。
答案 0 :(得分:1)
这看起来像个错误。你能用rspec-core提出问题吗?