我的类常量在“测试”中为零。环境

发布于 2024-09-25 13:36:22 字数 1277 浏览 2 评论 0原文

在一个名为“Quality”的类中,我定义了以下常量:

class Quality < ActiveRecord::Base

  [validations excluded in this example]

  NEW = Quality.find_by_name("New")
  LGT = Quality.find_by_name("Light use")
  MED = Quality.find_by_name("Medium use")
  HVY = Quality.find_by_name("Heavy use")
  SCR = Quality.find_by_name("Scrap")
  ANY = Quality.find_by_name("Any")

end

运行单元测试时,这些常量全部为零。为什么?我确信它们在生产/开发期间不是零,因为使用这些的代码似乎在我的开发/生产环境中工作。

我已经为这些记录设置了固定装置,因此我希望常量初始化能够正常工作。我的质量固定如下所示。这些装置位于“test/fixtures/quality.yml”的文件中

any:
  value: 0
  name: Any
  extended_name: /all

new:
  value: 5
  name: New
  extended_name:  (or like new)

lgt:
  value: 4
  name: Light use
  extended_name:  (cosmetic damange only)

med:
  value: 3
  name: Medium use
  extended_name:  (some functional damange)

hvy:
  value: 2
  name: Heavy use
  extended_name:  (needs work)

scr:
  value: 1
  name: Scrap
  extended_name: (only good for parts)

最后,这是我的单元测试,该测试失败并显示“Expected not nil”

test "all constant qualities are not nil" do
  assert_not_nil Quality::ANY
  assert_not_nil Quality::NEW
  assert_not_nil Quality::LGT
  assert_not_nil Quality::MED
  assert_not_nil Quality::HVY
  assert_not_nil Quality::SCR
end

In a class called 'Quality' I have the following constants defined:

class Quality < ActiveRecord::Base

  [validations excluded in this example]

  NEW = Quality.find_by_name("New")
  LGT = Quality.find_by_name("Light use")
  MED = Quality.find_by_name("Medium use")
  HVY = Quality.find_by_name("Heavy use")
  SCR = Quality.find_by_name("Scrap")
  ANY = Quality.find_by_name("Any")

end

When running my unit tests, these constants are all nil. Why? I'm sure they're not nil during production/development since the code that uses these seems to work in my dev/prod environments.

I've setup fixtures for these records, so I expect the constant initialization to work. My fixture for qualities appears below. These fixtures are in a file at 'test/fixtures/qualities.yml'

any:
  value: 0
  name: Any
  extended_name: /all

new:
  value: 5
  name: New
  extended_name:  (or like new)

lgt:
  value: 4
  name: Light use
  extended_name:  (cosmetic damange only)

med:
  value: 3
  name: Medium use
  extended_name:  (some functional damange)

hvy:
  value: 2
  name: Heavy use
  extended_name:  (needs work)

scr:
  value: 1
  name: Scrap
  extended_name: (only good for parts)

Finally, here's my unit test, which fails with 'Expected not nil'

test "all constant qualities are not nil" do
  assert_not_nil Quality::ANY
  assert_not_nil Quality::NEW
  assert_not_nil Quality::LGT
  assert_not_nil Quality::MED
  assert_not_nil Quality::HVY
  assert_not_nil Quality::SCR
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-10-02 13:36:22

这是因为当你的类加载时你的数据库是空的。所有装置均在类加载后插入。

如果您想要这种行为,我建议您在类属性中包含这些数据,效果会更好。

class

  def self.lgt
    @@lgt ||= Quality.find_by_name("Light use")
  end

  def self.med
    @@med ||= Quality.find_by_name("Medium use")
  end

  etc..

end

在这种情况下,您的数据仅加载 1 次,并且仅在您真正需要时加载。

test "all constant qualities are not nil" do
  assert_not_nil Quality.lgt
  assert_not_nil Quality.med
end

使用 Constante 来解决这个问题确实很糟糕。

it's because your database is empty when your class is loaded. All fixtures are insert after your class Loading.

If you want this behaviour I propose you to have this data in your class attributes it's better.

class

  def self.lgt
    @@lgt ||= Quality.find_by_name("Light use")
  end

  def self.med
    @@med ||= Quality.find_by_name("Medium use")
  end

  etc..

end

With this case you data is loaded only 1 time and only when you really need it.

test "all constant qualities are not nil" do
  assert_not_nil Quality.lgt
  assert_not_nil Quality.med
end

Using Constante for that is really bad solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文