gpt4 book ai didi

ruby - 如何在 irb 中使用 RSpec 期望

转载 作者:数据小太阳 更新时间:2023-10-29 06:43:51 25 4
gpt4 key购买 nike

我想在 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)

最佳答案

你很接近,但是在顶层调用 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

关于ruby - 如何在 irb 中使用 RSpec 期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com