比较rspec自定义ActiveRecord :: RecordInvalid错误消息

编程入门 行业动态 更新时间:2024-10-23 09:26:52
本文介绍了比较rspec自定义ActiveRecord :: RecordInvalid错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

模型中具有以下内容:

validates_uniqueness_of :title, if: proc { |item| item.item_type == 'tag' }, case_sensitive: false, message: I18n.t('errors.key', value: "%{value}")

并尝试在rspec中使用以下命令进行验证:

and trying to validate in rspec with the following :

expect { xxx }.to raise_error( ActiveRecord::RecordInvalid, I18n.t('errors.key', value: '...passing the title...') )

我被紧紧的跟随着:

ActiveRecord::RecordInvalid with "translated error", got #<ActiveRecord::RecordInvalid: Validation failed: translated error>

期望值等待一个没有报价的值,而发送的值带有报价;

The expectation awaits a value without quote, while the value sent has quote; it fails on that

我在默认设置下遇到了同样的错误:

I get the very same error with the default setup :

activerecord: errors: models: item: attributes: title: taken: 'translated error'

同一测试通过以下测试:

The same test passes with the followings :

期望{xxx}。以提高错误

期望{xxx}。以提高错误(ActiveRecord :: RecordInvalid)

使用默认设置也是如此:

So does using the default setup :

感谢任何帮助

推荐答案

ActiveRecord将前缀 Validation failed:添加到错误信息。相反,请在测试中尝试以下操作:

ActiveRecord adds the prefix Validation failed: to the error message. Try this in your test instead:

expect { xxx }.to raise_error( ActiveRecord::RecordInvalid, "Validation failed: " + I18n.t('errors.key', value: '...passing the title...') )

您可以通过在语言文件中设置以下键来更改此默认值:

You can change this default by setting the following key in your language file:

en: activerecord: errors: messages: record_invalid: "Validation failed: %{errors}"

更多推荐

比较rspec自定义ActiveRecord :: RecordInvalid错误消息

本文发布于:2023-10-23 22:50:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   错误   消息   rspec   RecordInvalid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!