Ruby RSpec应接收(:发送)。带有(…):彩色差异

Ruby RSpec应接收(:发送)。带有(…):彩色差异,ruby,unit-testing,testing,rspec,Ruby,Unit Testing,Testing,Rspec,我正在大量使用rspecs2.13应该接收(:发送)。带有(…)功能。我知道这是一个过时的版本,但我不得不在一个项目中使用它(testsuite非常庞大) 当规范失败时,我会得到如下的差异输出: <FooBar (class)> received :send with unexpected arguments expected: ({ :foo => bar, :test => "hello"}) got: ({ :foo =>

我正在大量使用rspecs2.13
应该接收(:发送)。带有(…)
功能。我知道这是一个过时的版本,但我不得不在一个项目中使用它(testsuite非常庞大)

当规范失败时,我会得到如下的差异输出:

<FooBar (class)> received :send with unexpected arguments
         expected: ({ :foo => bar, :test => "hello"})
         got: ({ :foo => bar, :test => "Hello"})
  FooBar.should_receive(:send) do |arg1|
    arg1.should == {}
  end.and_return(true)
但是,重写所有使用
should\u receive
语法的测试,以使用自定义匹配器,这将是一项艰巨的工作

有没有一种方法,或者是内置的,或者是gem,或者是一种重新定义
应该接收的
的方法,为失败的测试提供一些彩色的diff输出

编辑:

我找到了一个解决方法,它可以带来更好的差异输出。您可以使用如下块调用
应接收

<FooBar (class)> received :send with unexpected arguments
         expected: ({ :foo => bar, :test => "hello"})
         got: ({ :foo => bar, :test => "Hello"})
  FooBar.should_receive(:send) do |arg1|
    arg1.should == {}
  end.and_return(true)

但是,不带块的彩色diff输出仍然是有用的。

我相信Rspec将为阵列提供更好的diff输出。我不记得确切的语法,但例如

expected: [a, b, c]
got:      [a, d]
diff: - [b]
        [c]
      + [d]
根据您的具体情况,您可以将散列转换为一个数组来利用它

myHash.map{ |key, value| value}


我相信Rspec将通过降低成本为阵列提供更好的差异输出。我不记得确切的语法,但例如

expected: [a, b, c]
got:      [a, d]
diff: - [b]
        [c]
      + [d]
根据您的具体情况,您可以将散列转换为一个数组来利用它

myHash.map{ |key, value| value}