我的类常量在“测试”中为零。环境
在一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为当你的类加载时你的数据库是空的。所有装置均在类加载后插入。
如果您想要这种行为,我建议您在类属性中包含这些数据,效果会更好。
在这种情况下,您的数据仅加载 1 次,并且仅在您真正需要时加载。
使用 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.
With this case you data is loaded only 1 time and only when you really need it.
Using Constante for that is really bad solution.