是否有用于属性确切长度的rspec测试? [英] Is there an rspec test for exact length of an attribute?

查看:59
本文介绍了是否有用于属性确切长度的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测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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