首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails 2.3中运行单元测试时,SettingsLogic无法工作

在Rails 2.3中运行单元测试时,SettingsLogic无法工作
EN

Stack Overflow用户
提问于 2011-03-21 18:29:36
回答 1查看 565关注 0票数 1

当运行一些单元测试(使用Ruby/Rails中包含的Test::Unit模块)调用使用某些SettingsLogic特性的方法时,我会收到这个错误:

RuntimeError:将id命名为nil,这将错误地称为4 --如果您真的想要nil的id,请使用object_id

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

代码语言:javascript
复制
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

而在开发环境中,我得到:

代码语言:javascript
复制
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模型如下:

代码语言:javascript
复制
class Settings < Settingslogic
  source "#{Rails.root}/config/application.yml"
  namespace Rails.env
end

和application.yml设置文件:

代码语言:javascript
复制
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版本:Ruby1.8.7 (2010-06-23 patchlevel 299) i686Linux

RAILS版本: 2.3.10

设置逻辑: 2.0.6

谢谢!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-30 14:02:58

固定

问题是yaml中嵌入了红宝石:

代码语言:javascript
复制
Property.find_by_name('description').id

我没有属性固定装置,所以由于这个原因,它失败了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5381966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档