Ruby on Rails 测试:如何测试或至少查看 form_for 的 error_messages_for?
我正在创建一个测试,但我不明白为什么从 form_for 创建模型在测试中失败,但在真实的浏览器中却有效。有没有一种简单的方法可以让我了解模型创建中存在哪些问题?
更好的是,有没有一种简单的方法可以让我测试通过 error_messages_for 访问的错误输出?在这种情况下,我还想添加测试,以确保格式错误的表单输出正确的错误。
I'm working on creating a tests, and I can't figure out why the creation of a model from a form_for is failing in the test but works in real browsers. Is there a straightforward way for me to see what the problems are in the model creation?
Even better would be, is there a straightforward way for me to test the error outputs that I access via error_messages_for? In that case, I'd like to also add in tests that make sure that malformed forms are outputting the correct errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
@model.errors
访问对象的验证错误。使用@model.errors.on(:field_name)
获取应用于特定字段的错误消息数组。您还可以在测试中使用@model.errors.invalid?(:field_name)
来断言特定字段触发了错误。例如:
You can access the validation errors on an object using
@model.errors
. Use@model.errors.on(:field_name)
to get an array of error messages applied to a particular field. You can also use@model.errors.invalid?(:field_name)
in your tests to assert that an error was triggered for a particular field.For example: