简体   繁体   English

如何在Rails的测试夹具文件中访问模型中定义的哈希

[英]How to access a hash defined in a model in a test fixture file in rails

I have a Jobs model with a status_id attribute. 我有一个具有status_id属性的乔布斯模型。 I have the following hash defined in the model that represents the attribute, 我在代表属性的模型中定义了以下哈希,

  JOB_STATUS = {
    new: 10,
    scheduled: 20,
    in_progress: 30,
    complete: 40,
    declined: 50,
    cancelled: 60,
  }

In my fixture file, jobs.yml , I would like to use the hash as follows 在我的夹具文件jobs.yml ,我想按如下方式使用哈希

one:
  status_id: Job::JOB_STATUS[:scheduled]
  ... other model attributes

When I run my tests, status_id is always set to zero in the test database. 运行测试时,测试数据库中的status_id始终设置为零。

I think you need to stub in the value: 我认为您需要对值进行存根:

allow(Job).to receive(:JOB_STATUS).and_return ( JOB_STATUS = {
  new: 10,
  scheduled: 20,
  in_progress: 30,
  complete: 40,
  declined: 50,
  cancelled: 60,
} )

Try interpolating the value in yml 尝试插值yml中的值

one:
  status_id: <%= Job::JOB_STATUS[:scheduled] %>
  ... other model attributes

This should do 这应该做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM