所以,我的 ruby 代码中有一个模块,如下所示:
module MathStuff
class Integer
def least_factor
# implementation code
end
end
end
我有一些 RSpec 测试,我想在其中测试我的Integer#least_factor
方法按预期工作。为了简单起见,我们会说测试位于同一个文件中。测试看起来像这样:
describe MathStuff do
describe '#least_factor' do
it 'returns the least prime factor' do
expect(50.least_factor).to eq 2
end
end
end
不幸的是,当我运行测试时,我收到如下错误:
NoMethodError:
undefined method `least_factor' for 50:Fixnum
如果您知道如何包含,请告诉我MathStuff::Integer
用于测试的类。
注意:为了澄清起见,我实际上试图在此处打开 Ruby Integer 类并向其添加方法。
您的代码应如下所示:
describe MathStuff::Integer do
describe '#least_factor' do
it 'returns the least prime factor' do
expect(MathStuff::Integer.new.least_factor).to eq 2
end
end
end
但你在呼唤50.least_factor
50 是一个Fixnum
对象,不是你的MathStuff::Integer
并且它没有定义该方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)