Rails: Test::Unit 为什么我需要在任何其他断言之前断言某些内容是有效还是无效
当运行单元测试来检查我的标题长度是否>时,这看起来有点奇怪。 10 如果我包含“assert Product.invalid?”,我的测试就会通过。在我的任何其他断言之前:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "product title is too short" do
product = Product.new(:title => "My lady",
:description => "yyy",
:price => 1
)
assert product.invalid?
assert_equal "must be atleast 10 characters long.", product.errors[:title].join('; ')
end
end
但是,如果我不包括“断言product.invalid?”在assert_equal之前我得到这个错误 1)失败:test_product_title_is_too_short blah blah blah (“长度必须至少为 10 个字符。”) 是预期的结果,但结果是 (“”)。
这就是 Test::Unit 的工作原理吗?在进行其他测试之前,我必须断言某些内容是有效还是无效?有点像初始化测试?
This seems a little weird, when running a unit test to check if the length of my title is > 10 my test will pass if I include "assert product.invalid?" before any my other assert like this:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "product title is too short" do
product = Product.new(:title => "My lady",
:description => "yyy",
:price => 1
)
assert product.invalid?
assert_equal "must be atleast 10 characters long.", product.errors[:title].join('; ')
end
end
However if I don't include "assert product.invalid?" before assert_equal I get this error
1) Failure: test_product_title_is_too_short blah blah blah
("must be atleast 10 characters long.") expected but was ("").
Is that how Test::Unit works? I have to assert that something is valid or invalid before I proceed with other tests? Kind of like initializing the test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上不是测试框架的特征,而是 ActiveRecord 的特征。
使用 ActiveRecord 创建对象时,您可以分配验证以确保有关对象属性的某些信息(就像您对对象所做的那样)。但是,这些验证仅在特定时间运行,并且正如您所观察到的,“新”方法不是这些时间之一。但是,通过询问对象的有效性与无效性?方法,您因此触发了验证。
我认为更自然的方法是使用“create”方法而不是“new”来触发对象的验证。自动创建检查验证,这将消除您对“无效?”的调用。在您的测试中,仍应根据需要填充错误哈希:
与“create”方法类似的是“create!”如果任何验证失败,该方法实际上会引发异常。 create 将简单地返回 false 并填充错误哈希。
有关验证的更多信息,请查看:
http://guides.rubyonrails.org/active_record_validations_callbacks.html
This isn't actually a characteristic of your test framework, but rather of ActiveRecord.
When creating an object with ActiveRecord, you can assign validations to ensure certain things about the attributes of the objects (as you have on your object). However, these validations only get run at certain times, and as you're observing, the 'new' method isn't one of these times. However, by asking about the validity of the object with the invalid? method, you have thus triggered the validations.
I think what might be more natural is to use the "create" method instead of 'new' to trigger validations for your object. create checks validations automatically which will eliminate your call to "invalid?" in your test and should still populate the errors hash as desired:
Similarly to the 'create' method is the 'create!' method which will actually raise an exception if any validations fail. create will simply return false and populate the error hash.
For more info on validations check out:
http://guides.rubyonrails.org/active_record_validations_callbacks.html
这与 Test::Unit 无关。这是 Rails 功能。验证仅在调用
.valid?
或.save
版本之一时运行。您可以在此处阅读有关回调链的更多信息: http://guides.rubyonrails .org/active_record_validations_callbacks.html#when-does-validation-happenThis has nothing to do with Test::Unit. This is rails functionality. The validations only run when either
.valid?
or one of the versions of.save
are called. You can read more about the callback chain here: http://guides.rubyonrails.org/active_record_validations_callbacks.html#when-does-validation-happen