運行一些單元測試(使用包含的Ruby/Rails測試::單位模塊)調用使用一些SettingsLogic特徵的方法,當我得到這個錯誤:SettingsLogic不工作2.3
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
更具體地說,我寫了一些單元測試,以測試我的模型中的一些方法。這些方法使用SettingsLogic功能,但似乎在這裏發生了錯誤。我運行測試控制檯並檢查Settings類是否已定義。但是,我無法訪問我的設置值哈希值。
script/console test
Loading test environment (Rails 2.3.10)
DEPRECATION WARNING: require "activesupport" is deprecated and will be removed in Rails 3. Use require "active_support" instead. (called from /var/lib/gems/1.8/gems/activesupport-2.3.10/lib/activesupport.rb:2)
>> Settings
=> Settings
>> Settings.blah
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
from (erb):32
>> exit
而在開發環境中,我得到:
script/console
Loading development environment (Rails 2.3.10)
>> Settings
=> Settings
>> Settings.blah
=> {"gadget_type"=>{"alarm_history_table"=>{"max_printable_rows"=>200, "max_visible_rows"=>14, "_id"=>5, "rows_per_page"=>100}, "text"=>{"_id"=>2}, "current_state_table"=>{"max_printable_rows"=>200, "max_visible_rows"=>14, "_id"=>1, "rows_per_page"=>10}, "column_based_history_table"=>{"max_printable_rows"=>200, "max_visible_rows"=>14, "_id"=>6, "rows_per_page"=>100, "related_states"=>{"unit"=>17, "description"=>49}}, "entity_history_table"=>{"max_printable_rows"=>200, "max_visible_rows"=>14, "_id"=>4, "rows_per_page"=>100}, "chart"=>{"_id"=>3}, "access_type"=>{"url"=>1, "file"=>2}}}
>> exit
我手動運行測試,即不與耙試驗,但與ruby unit/my_model_test.rb
的settings.rb模型如下:
class Settings < Settingslogic
source "#{Rails.root}/config/application.yml"
namespace Rails.env
end
而application.yml設置文件:
defaults: &defaults
blah:
gadget_type:
access_type:
url: 1
file: 2
current_state_table:
_id: 1
max_visible_rows: 14
rows_per_page: 10
max_printable_rows: 200
text:
_id: 2
chart:
_id: 3
entity_history_table:
_id: 4
max_visible_rows: 14
rows_per_page: 100
max_printable_rows: 200
alarm_history_table:
_id: 5
max_visible_rows: 14
rows_per_page: 100
max_printable_rows: 200
column_based_history_table:
_id: 6
max_visible_rows: 14
rows_per_page: 100
max_printable_rows: 200
related_states:
description: <%= Property.find_by_name('description').id %>
unit: <%= Property.find_by_name('units').id %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
RUBY VERSION:紅寶石1.8.7(2010-06-23 PATCHLEVEL 299)[i686的Linux的]
鋼軌VERSION:2.3.10
設置LOGIC:2.0.6
謝謝!