gpt4 book ai didi

ruby - 如何使用 RSpec stub Array 实例方法

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

我在 String 类中有一个调用 Array 实例方法“shuffle”的实例方法。为了让我使用 RSpec 测试该方法,我想对其进行 stub 。

vowels = %w(a e i o u y)
vowels.shuffle

我试过:

Array.stub(:shuffle).and_return(%w(a e i o u y))
[].stub(:shuffle).and_return(%w(a e i o u y))

但都没有用 :( 我在这里遗漏了一些东西?也许是 mock ?但是我应该 mock 什么......

谢谢。

最佳答案

使用any_instance。例如:

describe "any_instance.stub" do
it "returns the specified value on any instance of the class" do
Array.any_instance.stub(:shuffle).and_return(%w(a e i o u y))

o = Array.new
o.shuffle.should eq(%w(a e i o u y))
end
end

Source

关于ruby - 如何使用 RSpec stub Array 实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292676/

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