是否有用于属性确切长度的rspec测试?

编程入门 行业动态 更新时间:2024-10-27 08:37:16
本文介绍了是否有用于属性确切长度的rspec测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试测试邮政编码属性的长度,以确保其5个字符长.现在,我正在测试以确保其不为空白,然后以4个字符表示太短,以6个字符表示太长.

I'm trying to test the length of a zip code attribute to ensure its 5 characters long. Right now I'm testing to make sure its not blank and then too short with 4 characters and too long with 6 characters.

有没有一种方法可以测试它是否正好是5个字符?到目前为止,我在网上或rspec书中都找不到任何东西.

Is there a way to test it being exactly 5 characters? So far I've found nothing online or in the rspec book.

推荐答案

如果要在ActiveRecord模型上测试验证,建议您尝试 shoulda-matchers .它提供了许多对Rails有用的有用的RSpec扩展.您可以为邮政编码属性编写一个简单的单行规范:

If you're testing a validation on an ActiveRecord model, I recommend trying out shoulda-matchers. It provides a bunch of useful RSpec extensions useful for Rails. You could write a simple one line spec for your zip code attribute:

describe Address do it { should ensure_length_of(:zip_code).is_equal_to(5).with_message(/invalid/) } end

更多推荐

是否有用于属性确切长度的rspec测试?

本文发布于:2023-08-05 20:43:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:确切   长度   属性   测试   rspec

发布评论

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

>www.elefans.com

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