测试“验证...,格式=>与”(Testing “validates …, format => with”)

系统教程 行业动态 更新时间:2024-06-14 17:00:14
测试“验证...,格式=>与”(Testing “validates …, format => with”)

我有一个用户模型进行此验证

validates :name, :lastname, :format => {:with => /^[a-zA-Z]+$/, :message => 'Only letters and spaces allowed.'}

我不确定如何正确测试它。

我做了一个函数,它返回一个由a-zA-z字符数组中的10个字符组成的随机字符串。

def get_random_name "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split('').shuffle[0..10].join

结束

然后我为每个规格运行获得一个新名称。

我不想测试它使用了一些正则表达式,因为那样我就会测试实现而不是行为,而且我也不想测试一个硬编码的情况。

我的问题是:我应该这样做吗? 真的需要吗? 是更好还是无用? 你知道有更好的方法来测试这种验证吗?

编辑:另一个问题,如何产生无效的随机名称? 有没有一种方法可以创建包含至少一个允许值之外的字符的随机名称? 我不能硬编码所有无效值的数组来随机化它,因为它太大了

I have a User model with this validation

validates :name, :lastname, :format => {:with => /^[a-zA-Z]+$/, :message => 'Only letters and spaces allowed.'}

I'm not sure how to properly test it.

I've done a function that returns a random string made by 10 chars from an array of a-zA-z chars.

def get_random_name "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split('').shuffle[0..10].join

end end

and then I get a new name for each run of my specs.

I don't want to test that it uses some regexp, because then I would be testing the implementation and not the behavior, and I also don't want to test only one hardcoded case.

My questions are: Should I do that? Is it really needed? Is it better or useless? Do you know any better way to test that kind of validations?

EDIT: another question, what about generating invalid random names? is there a way to create random names that includes at least one char outside the allowed values? I can't hardcode an array of all invalid values to randomize it since it would be TOO big

最满意答案

随机数据测试是一种常见的测试技术,称为模糊测试。 我会考虑使用FuzzBert的生成器来创建真正的随机二进制数据来测试。

这里有一些很好的随机数据的例子,你不想越过你的验证。

irb> require 'fuzzbert' => true irb> test_string = FuzzBert::Generators.random_fixlen(10)[] => "S\x1EU1\x11HVY\x0E\xD0" irb> puts test_string S▲U1◄HVY♫� => nil

随机是好的

因为它将随机二进制位转换为字符串,所以您将得到一些非常时髦的结果来进行测试。 这是一件好事! 它不仅会测试使用像?这样的已知符号,还会测试有效和无效字符的各种组合。

随机有时是有效的

您有可能偶尔获得有效数据,尽管不太可能。 此外,获取有效数据的几率越低,您创建的随机字符串就越长。

解决这个问题的第一个尝试可能是为每个输出添加一个无效字符,但我不会建议它。 例如,如果你总是附加“!”,那么它会使你的测试等同于确保字符串没有“!” 在它,而不是对完整正则表达式可能性的真正考验。

我会建议测试你的随机字符串对同样的正则表达式,如果它通过它,生成一个不同的字符串来测试。

最后,你可以忽略随机数据有效的难得机会。 如果每一次这个特定的rspec失败,请查看失败的字符串,如果它是一个有效的字符串,那么只需重新运行。

测试所有的可能性

您永远无法测试所有无效字符串(除非您的最大长度很短),但通过使用Fuzz Testing,您将能够测试包含大量有效和无效字符的字符串。

Testing against random data is a common testing technique called Fuzzing. I would look at using FuzzBert's Generators to create true random binary data to test against.

Here is an example of some great random data that you would not want getting past your validations.

irb> require 'fuzzbert' => true irb> test_string = FuzzBert::Generators.random_fixlen(10)[] => "S\x1EU1\x11HVY\x0E\xD0" irb> puts test_string S▲U1◄HVY♫� => nil

Random Is Good

Because it is converting random binary bits to strings, you are going to get some really funky results to test against. That is a good thing! Not only will it test against using known symbols like ?, but also all kinds of combinations of valid and invalid characters.

Random is sometimes valid

There is a chance you will get valid data every now and then, although very unlikely. Also, the odds of getting valid data decrease the longer the random string you create is.

A first attempt to solve this problem could be to just append an invalid character to every output, but I wouldn't suggest it. For example, if you always append "!", then it would make your test equivalent to making sure the string doesn't have a "!" in it and not a true test of the full regex possibilities.

I would suggest testing your random strings against that same regex, if it does pass it, generate a different string to test with.

Finally, you could just ignore the rare chance that the random data is valid. If every once-in-a-while this particular rspec fails, look at what string it failed on, if it was a valid string, then just rerun.

Test all the possibilities

You will never be able to test all non-valid strings (unless you have a short maximum length), but by using Fuzz Testing you will be able to test against strings that contain a large variety of valid and invalid characters.

更多推荐

本文发布于:2023-04-18 00:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/52fffbfce11e527f5a951f75c0a9c9a6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   测试   Testing   validates   format

发布评论

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

>www.elefans.com

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