简体   繁体   English

这个长度验证如何在Ruby on Rails中运行?

[英]How does this length validation work in Ruby on Rails?

I am following the Rails tutorial by Michael Hartl. 我正在关注Michael Hartl的Rails教程。 In chapter 6 we're creating a length validation test for a users name and email. 第6章中,我们将为用户名和电子邮件创建长度验证测试。

In the test/models/user_test.rb file he says to write 在test / models / user_test.rb文件中,他说要写

test "name should not be too long" do
@user.name = "a" * 51
assert_not @user.valid? 
end

and then in app/models/user.rb we put 然后在app / models / user.rb中我们放了

class User < ActiveRecord::Base 
validates :name,  presence: true, length: { maximum: 50 }

My question is, how does the test ensure that the name is not, for example, 60 characters long? 我的问题是,测试如何确保名称不是,例如,60个字符长? I get that the validation says to make the max length 50, but the test says assert that the user is not valid if user.name EQUALS 51 characters...not greater than or equal to. 我得到验证说最大长度为50,但是如果user.name EQUALS 51个字符...不大于或等于,则测试表明用户无效。

To be completely honest, I don't understand the relationship between why you need the validates in user.rb and then also the test file, so that could be why I'm confused. 说实话,我不明白你为什么需要user.rb中的验证与测试文件之间的关系,所以这可能就是为什么我感到困惑。

In tests, you need to ensure that your code does everything as you expect. 在测试中,您需要确保代码按预期执行所有操作。 In this case that you can't save the user with name which is longer than 50 symbols. 在这种情况下,您无法保存name超过50个符号的用户。 Thats why in test you are checking that user becomes invalid with name length == 51 symbol. 这就是为什么在测试中你检查用户变得无效的name长度== 51符号。

But you are also correct that this test doesn't guarantee that user with name length 60 will be invalid too. 但你也是正确的,这个测试并不能保证名字长度为60的用户也是无效的。 It also doesn't check that 50 is the maximum, because it will pass for 它也没有检查50是否是最大值,因为它会通过

validates :name,  presence: true, length: { maximum: 1 }

for example. 例如。 But you probably don't want your app to behave like this. 但是你可能不希望你的应用程序像这样。 That's why I also encourage you to add another check for maximum allowed length: 这就是为什么我还鼓励你为最大允许长度添加另一个检查:

@user.name = "a" * 50
assert @user.valid?

Usually, if you are doubt that something can be wrong in your code, you are free to add new tests. 通常,如果您怀疑代码中存在某些错误,您可以自由添加新测试。 But in this case, you shouldn't actually test how the code behaves, because presence/length validations are well-tested in Rails itself. 但在这种情况下,您实际上不应该测试代码的行为方式,因为存在/长度验证在Rails本身中经过了充分测试。 You should just check that you included such validations in your model with correct arguments passed. 您应该检查是否在模型中包含了这些验证,并且传递了正确的参数。 For example, in shoulda-matchers you have these helpers: 例如,在shoulda-matchers中你有这些助手:

should validate_presence_of(:name)
should validate_length_of(:name).is_at_most(50)

I am not sure if unit-test have the analogs, most likely no, so you should test it yourself in the way you do this and assume this is enough. 我不确定单元测试是否有类似物,很可能没有,所以你应该按照你的方式自己测试并假设这已经足够了。

The test is just asserting that a user name with 51 characters should not be a valid one. 测试只是断言51个字符的用户名不应该是有效的。

The test doesn't 'ensure' that the user name can't be 60 characters long. 测试不“确保”用户名不能超过60个字符。 That's what the actual validation code does. 这就是实际验证代码的作用。

For example, if you were to change the validation code to this: 例如,如果您要将验证代码更改为:

class User < ActiveRecord::Base 
validates :name,  presence: true, length: { maximum: 60 }

then the test would fail because the code is validating a user name with 51 characters. 然后测试将失败,因为代码正在验证具有51个字符的用户名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM