我正在关注Rspec的Lynda教程。我正在解决其中一个挑战,但似乎并没有起作用。 我的文件结构是:
food_finder / LIB /支撑/ number_helper.rb
food_finder /规格/支持/ number_helper_spec.rb
我的number_helper.rb看起来像是:
module NumberHelper
def number_to_currency(number, options={})
#some_code
end
end
和number_helper_spec.rb是:
describe 'NumberHelper' do
include NumberHelper
describe '#number_to_currency' do
#some_test_code
end
end
执行表格food_finder目录:
rspec spec/support/number_helper_spec.rb
我收到错误:
:未初始化的常量NumberHelper (NameError)
答案 0 :(得分:0)
你的代码很好。该模块应加载include NumberHelper
,而不是require 'support/number_helper'
。问题来自于没有从应用程序的根目录运行rspec --init
,因此没有加载spec_helper.rb中的任何配置。
关于提到Rails'number_to_currency
方法的注释,本练习中加载的模块是在Rails版本之后建模的,但是是自包含的:
<强> number_helper.rb 强>
# This module illustrates how additional functionality can be
# included (or "mixed-in") to a class and then reused.
# Borrows heavily from Ruby on Rails' number_to_currency method.
module NumberHelper
def number_to_currency(number, options={})
unit = options[:unit] || '$'
precision = options[:precision] || 2
delimiter = options[:delimiter] || ','
separator = options[:separator] || '.'
...