加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:06:28 所属栏目:百科 来源:网络整理
导读:我想使用[1,2,3].应该包括(1)在irb中.我试过了: ~$irb1.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 Mod
我想使用[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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读