Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么这次考试会通过?如此困惑_Ruby On Rails - Fatal编程技术网

Ruby on rails 为什么这次考试会通过?如此困惑

Ruby on rails 为什么这次考试会通过?如此困惑,ruby-on-rails,Ruby On Rails,我遵循Hartl的RoR教程,在第6章中遇到了以下测试代码,这是没有意义的: require 'test_helper' class UserTest < ActiveSupport::TestCase def setup @user = User.new(name: "Example User", email: "user@example.com") end test "should be valid" do assert @user.valid?

我遵循Hartl的RoR教程,在第6章中遇到了以下测试代码,这是没有意义的:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

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

  test "should be valid" do
    assert @user.valid?
  end

  test "name should be present" do
    @user.name = "     "
    assert_not @user.valid?
  end
end
需要“测试助手”
类UserTest
关注最后一个测试“name应该存在”,将@user.name变量分配为空格。那么,这会不会让测试失败呢

第二,什么不是?Assert类似于“检查这是否为真”。 那么assert_不是“检查它是否为false”吗

那么上面的测试是在做以下工作

  • @名称被设置为空白

  • 检查@name是否无效

  • 是,这是无效的,因为@name在步骤1中被设置为空格

  • 如此困惑

    在rails中被定义为“非空”--并且被定义为“空的或仅包含空白”。因此,用所有空格填充的用户名将不存在。assert_not与assert相反-它要求表达式为false。因此,假设您的用户对象正在验证name属性的存在性,那么用空格填充它将使该名称不存在,从而使该对象无效,并且assert_不应通过