简体   繁体   English

为什么我不理解Rails从教程中验证在线状态?

[英]Why don't I understand the Rails validates presence from tutorial?

I am following a Rails tutorial, and we created a user model, then were testing it with RSpec. 我正在关注Rails教程,我们创建了一个用户模型,然后使用RSpec对其进行了测试。 In the spec/model/user_spec.rb we have: 在spec / model / user_spec.rb中,我们有:

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end
end

When running the test it fails. 运行测试时失败。 When we add the following in the user model: 当我们在用户模型中添加以下内容时:

validates :name, presence: true

it passes. 它通过了。 I understand that. 我明白那个。 What I don't understand is the use of this: 我不明白的是它的用法:

describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
 end

Isn't the validate presence enough? 验证存在还不够吗?

After your edits, I think you are asking, "if we already checked that name is present to validate a record in the model class, why are we checking it again in a test?" 编辑完之后,我想您是在问:“如果我们已经检查过存在的name以验证模型类中的记录,为什么还要在测试中再次检查它呢?” The simple but not really clear answer is that the first part is checking that a user has a name, and the second part is testing that your application performs this check when actually presented with a new user. 一个简单但不是很明确的答案是,第一部分是检查用户是否具有名称,第二部分是测试您的应用程序在实际呈现给新用户时是否执行此检查。

The validates :name line is checking to see if a value for the name attribute is present; validates :name行将检查name属性值是否存在; if not, Rails won't save the record when the save method is called on an instance of that model class, and it will generate an error message. 如果不是,则在该模型类的实例上调用save方法时,Rails不会保存该记录,并且它将生成一条错误消息。

The spec is testing to validate that if you try to assign a blank or nil value to the attribute that must be validated ( name ), that Rails will not consider it valid, thus preventing it from being saved and triggering error messages. 规范正在测试以验证是否尝试为必须验证的属性( name )分配空白或nil值,Rails不会将其视为有效,从而防止其被保存并触发错误消息。

There is an argument to be made that this is overkill as a test; 有一种观点认为,作为一种考验,这是过分杀伤力的。 in one sense you are just testing that functionality built into Rails (the validates method), which is presumably already tested there, is working as expected. 从某种意义上说,您只是在测试Rails内建的功能( validates方法)(预期已经在此处进行了测试)是否按预期工作。

On the other hand, if you are writing the tests first and using them to describe the intended behavior of the User model, then you can argue that you are testing the application behavior associated with an attempt to create an invalid User to confirm it works as intended. 另一方面,如果您首先编写测试并使用它们来描述User模型的预期行为,则可以认为您正在测试与试图创建无效User来确认其正常工作相关的应用程序行为。意。 I generally agree with this latter point of view. 我大体上同意后一种观点。

The following line adds some validation to the User model: 以下行为User模型添加了一些验证:

validates :name, presence: true

This tells Rails that the name attribute must be present for an instance of the User model to be valid. 这告诉Rails必须存在name属性,以使User模型的实例有效。 If an instance of a model is not valid it cannot be saved ... and through this methodology you can prevent invalid records being saved. 如果模型的实例无效,则无法保存...,通过这种方法可以防止保存无效记录。

As you progress through the tutorial this will begin to make more sense as you see the validation errors in the forms. 随着教程的逐步进行,当您在表格中看到验证错误时,这将变得更加有意义。

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

相关问题 Michael Hartl Rails 教程:assert_not 与它应该做的完全相反,我不明白为什么 - Michael Hartl Rails tutorial: assert_not does exact opposite of what it should and I don't understand why 试图了解为什么validates_presence_of测试失败 - Trying to understand why validates_presence_of test fails 不了解RailsCast教程中的一些代码 - don't understand some code from a RailsCast tutorial Rails验证不适用于更新 - Rails Validates don't work on update 如何在Rails的实例上运行validates_presence_of? - How do i run validates_presence_of on an instance in rails? 我不明白为什么带options_from_collection_for_select的f.select的Ruby on Rails代码不起作用 - I don't understand why this Ruby on Rails code for f.select with options_from_collection_for_select does not work 是否已经不赞成`validates_presence_of`(从Rails 3开始)? - Is `validates_presence_of` deprecated (as of Rails 3)? 验证是否存在无法在Rails 2.3.8中工作的情况 - Validates presence of not working in Rails 2.3.8 我不明白这个SyntaxError。 为什么/ app / app / ...? 它发生在Heroku上,适用于rails服务器 - I don't understand this SyntaxError. Why /app/app/…? It happens on Heroku, works on rails server 我不明白为什么这是XSS警告Rails4。link_to href中的不安全模型属性 - I don't understand why this is an XSS warning Rails 4. Unsafe model attribute in link_to href
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM