【问题标题】:How to use RSpec expectations in irb如何在 irb 中使用 RSpec 期望
【发布时间】:2013-02-07 10:38:17
【问题描述】:

我想在 irb 中使用 [1,2,3].should include(1)。我试过了:

~$ irb
1.9.3p362 :001 > require 'rspec/expectations'
 => true 
1.9.3p362 :002 > include RSpec::Matchers
 => Object 
1.9.3p362 :003 > [1,2,3].should include(1)
TypeError: wrong argument type Fixnum (expected Module)
    from (irb):3:in `include'
    from (irb):3
    from /home/andrey/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `<main>'

但是it's a valid case 不起作用。如何使用[1,2,3].should include(1)

【问题讨论】:

    标签: ruby rspec irb rspec-expectations


    【解决方案1】:

    您很接近,但在顶层调用 include 您将调用 Module#include。要绕过它,您需要删除原始的 include 方法,以便调用 RSpec 的 include

    首先让我们弄清楚include这个系统是从哪里来的:

    > method :include
    => #<Method: main.include>
    

    好的。看起来它是在main 中定义的。这是 Ruby 顶级对象。所以让我们重命名并删除原来的包含:

    > class << self; alias_method :inc, :include; remove_method :include; end
    

    现在我们可以开始谈正事了:

    > require 'rspec'
    > inc RSpec::Matchers
    > [1,2,3].should include(1)
    => true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多