RSpec的 - 如何检查allow_blank存在 [英] rspec - how to check that allow_blank exists

查看:205
本文介绍了RSpec的 - 如何检查allow_blank存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有唯一性测试,如下:

I have a test for uniqueness which works:

it { should validate_uniqueness_of(:name).case_insensitive }

但是当我尝试做一个类似的测试名称失败

however when I try to do a similar test for name it fails

it { should validate_presence_of(:name).allow_blank }    

我得到这个错误:未定义的方法 allow_blank'`

I get this error: undefined methodallow_blank'`

推荐答案

根据这个名单,还有在早该没有一种方法可以链接到validate_ presence_of,将支持你想做的事情。 看一看<一href="https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/validate_$p$psence_of_matcher.rb"相对=nofollow>的匹配源$ C ​​$ C这里。

According to this list, there's no method in shoulda that can be chained to validate_presence_of that would support what you want to do. Have a look at the source code of the matcher here.

然而,还有另外一个匹配器可以使用:

However, there is another matcher you can use:

it { should allow_value("", nil).for(:name) }

在此进行测试,如果为空值,可以应用到你的属性。

Which tests if blank values can be applied to your attribute.

然后,完整的测试将是

it { should validate_presence_of(:name) }    
it { should allow_value("", nil).for(:name) }

这篇关于RSpec的 - 如何检查allow_blank存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆