我正在尝试学习rspec并用它编写一个hello-world程序。但是看来我不能正确地要求我上课。它始终显示未定义的方法错误。这是我的文件结构:
├── lib
│ └── map.rb
└── spec
├── map_spec.rb
└── spec_helper.rb
和我的map_spec.rb文件:
require 'map'
describe 'Map' do
it 'should iterate over array and return new array based on some simple addition' do
expect([1,2,3]).map_sam_mario {|e| e+1}.to eq [2,3,4]
end
end
这是我的map.rb文件:
class Array
def map_sam_mario
[2,3,4]
end
end
当我在当前目录中执行rspec时,它总是显示此错误:
NoMethodError:
undefined method `map_sam_mario' for # <RSpec::Expectations::ExpectationTarget:0x007fad5a9e8270>
我进行了截屏,并编写了与视频完全相同的代码。我不知道为什么会这样。我正在使用ruby 2.1.0和rspec 3.0.3
expect([1,2,3]).map_sam_mario {|e| e+1}.to eq [2,3,4]
你叫map_sam_mario
上expect
。原因是没有这样的方法,因为这是RSpec::Expectations::ExpectationTarget
类。
这行应该是这样的:
expect([1,2,3].map_sam_mario {|e| e+1}).to eq [2,3,4]
因此map_sam_mario
应在您的计算机上调用Array
。
此错误与无关require
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句