所以,我的 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 类并向其添加方法。