【问题标题】:SettingsLogic not working when running unit tests in Rails 2.3在 Rails 2.3 中运行单元测试时,SettingsLogic 不起作用
【发布时间】:2011-07-19 21:53:54
【问题描述】:

在运行一些单元测试(使用 Ruby/Rails 中包含的 Test::Unit 模块)调用使用某些 SettingsLogic 功能的方法时,我收到此错误:

RuntimeError: Called id for nil, 错误地是 4 -- 如果你 真的想要 nil 的 id,使用 object_id

更具体地说,我编写了一些单元测试来测试我的模型中的一些方法。这些方法使用 SettingsLogic 功能,但似乎这里发生了一些错误。我运行了一个测试控制台并检查了设置类是否已定义。但是,我无法访问我的设置值哈希。

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

我正在手动运行测试,即不是使用 rake 测试而是使用 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 版本:ruby 1.8.7(2010-06-23 补丁级别 299)[i686-linux]
导轨版本:2.3.10
设置逻辑:2.0.6
谢谢!!

【问题讨论】:

    标签: ruby-on-rails ruby settingslogic


    【解决方案1】:

    已修复

    问题在于 yaml 中嵌入的 ruby​​:

    Property.find_by_name('description').id
    

    我没有属性固定装置,因此它失败了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 2014-03-28
      • 2019-12-27
      • 1970-01-01
      相关资源
      最近更新 更多