Ruby 在RSpec中,使用let分配正则表达式会创建意外的通过/失败行为-Bug还是用户错误?

Ruby 在RSpec中,使用let分配正则表达式会创建意外的通过/失败行为-Bug还是用户错误?,ruby,rspec,Ruby,Rspec,带有测试对象的文件:foo.rb class Foo def a_string "abcdef8" end end 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

带有测试对象的文件:
foo.rb

class Foo
  def a_string
    "abcdef8"
  end
end
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
.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
规范文件:
foo_Spec.rb

class Foo
  def a_string
    "abcdef8"
  end
end
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
.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
。我第一次注意到这个问题是在我检查我的匹配器时(即
p my\u matcher
),但它也发生在调用
my\u matcher

这是一个错误,还是我做错了什么?也许它与捕获正则表达式数据有关

这似乎是不可思议的奇怪行为,尤其是对ruby来说


不管它值多少钱,这是一个简单的(如果稍微少一点干)修复。如果在
it
块中声明了
my\u matcher
,它将按预期工作。

这看起来像个bug。您可以向rspec core提交一个问题吗?

看起来像个bug。您可以向rspec core提交问题吗?

可以,我将提交错误报告。知道是什么原因吗?我更愿意接受迈伦的回答,因为他在幕后挖掘和孤立了这个问题。他在1.9.3中创建了一个看起来像ruby bug的东西(ruby 2.0没有这个问题)。是的,我会提交一个bug报告。知道是什么原因吗?我更愿意接受迈伦的回答,因为他在幕后挖掘和孤立了这个问题。他在1.9.3中创建了一个看起来像ruby bug的东西(ruby 2.0没有这个问题)。