Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord 验证的长度\u时是否应出现匹配器错误_Activerecord_Rspec_Ruby On Rails 3.2_Rspec Rails_Shoulda - Fatal编程技术网

Activerecord 验证的长度\u时是否应出现匹配器错误

Activerecord 验证的长度\u时是否应出现匹配器错误,activerecord,rspec,ruby-on-rails-3.2,rspec-rails,shoulda,Activerecord,Rspec,Ruby On Rails 3.2,Rspec Rails,Shoulda,我正在为我的用户模型编写一些规范,但是测试没有通过。我在gem的github页面上闲逛,但没有成功。这是我的代码: validates :phone, numericality: { only_integer: true }, allow_blank: true, length: { is: 7 } validates :mobile, numericality: { only_integer: true }, allow_blank: true, length: { is: 10 } 还有我

我正在为我的用户模型编写一些规范,但是测试没有通过。我在gem的github页面上闲逛,但没有成功。这是我的代码:

validates :phone, numericality: { only_integer: true }, allow_blank: true, length: { is: 7 }
validates :mobile, numericality: { only_integer: true }, allow_blank: true, length: { is: 10 }
还有我的眼镜

it { should ensure_length_of(:phone).is_equal_to(7) }
it { should ensure_length_of(:mobile).is_equal_to(10) }
此验证应仅在更新过程中运行

错误:

当telefono设置为“xxxxxxx”时,不希望错误包括“longitud errónea(debe ser de 7个字符)”,得到错误:longitud errónea(debe ser de 7个字符 德7克拉)

当celular设置为“XXXXXXXXX”时,不希望错误包括“longitud errónea(debe ser de 10个字符)”,得到错误:longitud errónea(debe s (10克拉)

仅供参考:我已经设置了西班牙地区;我想这就是原因


希望有人能帮助我。Thank is advance

我认为您得到了错误,因为shoulda_matcher不希望
length:{is:7}
数值性一起使用


PS对于我来说-使用数字作为电话号码是很奇怪的,你可以使用字符串并使用
验证
格式
来确保数据的有效性。例如,电子邮件验证

谢谢@gotva。我是rails的新手,我认为电话和手机号码验证必须具有数字性。我喜欢给未来的读者看的东西