如何在irb中使用RSpec期望 [英] How to use RSpec expectations in irb

查看:86
本文介绍了如何在irb中使用RSpec期望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd want to use [1,2,3].should include(1) in irb. I tried:

~$ 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>'

但是,尽管这是有效的案例,但它不起作用.如何使用[1,2,3].should include(1)?

But it doesn't work though it's a valid case. How can I use [1,2,3].should include(1)?

推荐答案

您很近,但是在顶层调用include时,您将在调用Module#include.要解决此问题,您需要删除原始的include方法,以便改为调用RSpec的include.

You are close, but calling include on top-level you will be calling Module#include. To get around it you need to remove the original include method so that RSpec's include gets called instead.

首先让我们弄清楚系统include的来源:

First let's figure out where the system include comes from:

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

好的.看起来它是在main中定义的.这是Ruby的顶级对象.因此,让我们重命名并删除原始包含:

Ok. It looks like it's defined in main. This is the Ruby top-level object. So let's rename and remove the original include:

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

现在我们可以开始做生意了

Now we can get down to business:

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

这篇关于如何在irb中使用RSpec期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆