ruby – 如何在irb中使用RSpec的期望

我想使用[1,2,3].应该包括(1)在irb中.我试过了:
~$irb
1.9.3p362 :001 > require 'rspec/expectations'
 => true 
1.9.3p362 :002 > include RSpec::Matchers
 => Object 
1.9.3p362 :003 > [1,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,3]应包括(1)?

解决方法

你很接近,但是打电话包括在顶层你会调用Module#include.要解决它,您需要删除原始的include方法,以便RSpec的include被调用.

首先我们来弄明白系统的来源:

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

好.它看起来像在主要定义.这是Ruby的顶级对象.所以我们来重命名删除原来的包括

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

现在我们可以下定决心:

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...