使用 Rails 固定装置进行单元测试
抱歉,问题标题含糊不清,但我很难将我所要求的内容提炼成一行...
我有几个通过rails gscaffold生成的简单Rails(3.1)模型,我正在尝试对其进行单元测试。它们的定义方式如下:
class ModelA < ActiveRecord::Base
validates_presence_of :field1, :field2
validates_uniqueness_of :field1
end
class ModelB < ActiveRecord::Base
validates_presence_of :field1
validates_uniqueness_of :field1
end
我每个模型都有几个固定装置,即:
model_a_no_field1:
field2: test
model_a_no_field2:
field1: test
model_a_ok:
field1: test
field2: test
我
model_b_no_field1:
model_b_ok:
field1: test
的单元测试正在测试这些验证:
class ModelATest < ActiveSupport::TestCase
test "field1 should be present" do
assert !model_as(:model_a_no_field1).valid?
end
test "field2 should be present" do
assert !model_as(:model_a_no_field2).valid?
end
test "field1 should be unique" do
model_a = model_as(:model_a_ok)
model_a.save
assert !model_as(:model_a_ok).valid?
end
end
这些测试都正确通过。但是,我对 ModelB 的类似单元测试:
class ModelBTest < ActiveSupport::TestCase
test "field1 should be present" do
assert !model_bs(:model_b_no_field1).valid?
end
test "field1 should be unique" do
model_b = model_bs(:model_b_ok)
model_b.save
assert !model_bs(:model_b_ok).valid?
end
end
第二次测试失败(测试唯一性)。
我几乎可以肯定这与 model_b_no_field1
的空 YAML 固定装置有关,但我不确定。我可以通过将测试方法主体替换为
test "field1 should be unique" do
model_b = model_bs(:model_b_ok)
model_b.save
model_b2 = ModelB.new
assert !model_b2.valid?
end
Which is Fine 来使测试通过,但我想了解这里发生了什么。
Sorry for the vague question title, but I'm having a hard time trying to distil what I'm asking into a one-liner...
I have a couple of simple Rails (3.1) models generated via rails g scaffold
, which I'm trying to unit test. They are defined in the following way:
class ModelA < ActiveRecord::Base
validates_presence_of :field1, :field2
validates_uniqueness_of :field1
end
class ModelB < ActiveRecord::Base
validates_presence_of :field1
validates_uniqueness_of :field1
end
I have a couple of fixtures for each model, i.e:
model_a_no_field1:
field2: test
model_a_no_field2:
field1: test
model_a_ok:
field1: test
field2: test
and
model_b_no_field1:
model_b_ok:
field1: test
My unit tests are testing these validations:
class ModelATest < ActiveSupport::TestCase
test "field1 should be present" do
assert !model_as(:model_a_no_field1).valid?
end
test "field2 should be present" do
assert !model_as(:model_a_no_field2).valid?
end
test "field1 should be unique" do
model_a = model_as(:model_a_ok)
model_a.save
assert !model_as(:model_a_ok).valid?
end
end
These tests all pass correctly. However, my similar unit tests for ModelB
:
class ModelBTest < ActiveSupport::TestCase
test "field1 should be present" do
assert !model_bs(:model_b_no_field1).valid?
end
test "field1 should be unique" do
model_b = model_bs(:model_b_ok)
model_b.save
assert !model_bs(:model_b_ok).valid?
end
end
Fail on the second test (testing the uniqueness).
I'm almost certain this is to do with the empty YAML fixture for model_b_no_field1
, but I'm not sure. I can cause the test to pass by replacing the test method body with
test "field1 should be unique" do
model_b = model_bs(:model_b_ok)
model_b.save
model_b2 = ModelB.new
assert !model_b2.valid?
end
Which is fine, but I'd like to understand what's happening here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你正在颠覆 Rails 为你创建的测试基础设施。 yaml 文件中的模型应该是有效的,如果您想测试无效的模型,请在测试中创建它。
在测试中创建数据具有非常清晰和具体的额外价值。否则,开发人员需要打开 yaml 文件来了解您拥有哪些数据以及缺少哪些数据。
I think you're subverting the test infrastructure that rails creates for you. The models that you have in the yaml file should be valid, and if you want to test something that is invalid, create it in the test.
Creating the data in the test has the extra value of being very clear and concrete. Otherwise a dev would need to open the yaml file to understand what data you have and what is missing.