简体   繁体   English

Ruby on Rails用户测试失败

[英]Ruby on Rails User Test Fails

Doing the Ruby on Rails Tutorial and I'm getting strange error message when running the test. 在进行Ruby on Rails教程时,运行测试时出现奇怪的错误消息。 My user_test.rb: 我的user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def setup
    @user = User.new(name: "Example User", email: "user@example.com", password: "wordpass", password_confirmation: "wordpass")
  end
  test "should be valid" do
    assert @user.valid?
  end

  test "name should be present" do
    @user.name="      "
    assert_not @user.valid?
  end

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

  test "email should not be too long" do
    @user.email= "a" * 256
    assert_not @user.valid?
  end
  test "email validation should accept valid addresses" do
    valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
                         first.last@foo.jp alice+bob@baz.cn]
    valid_addresses.each do |valid_address|
      @user.email = valid_address
      assert @user.valid? , "#{valid_address.inspect} should be valid"
    end
  end

  test "email validation should reject invalid addresses" do
    invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
                           foo@bar_baz.com foo@bar+baz.com]
    invalid_addresses.each do |invalid_address|
      @user.email = invalid_address
      assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
    end
  end
  test "email addresses should be unique" do
    duplicate_user = @user.dup
    duplicate_user.email = @user.email.upcase
    @user.save
    assert_not duplicate_user.valid?
  end
  test "password should be a minimum length" do
    @user.password = @user.password_confirmation = "a" * 5
    assert_not @user.valid?
  end

end

The user.rb : user.rb:

class User < ActiveRecord::Base

    attr_accessor :name, :email , :password, :password_confirmation
    before_save { self.email = email.downcase }  
    validates :name,  presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
            uniqueness: { case_sensitive: false }
    has_secure_password
    validates :password, length: { minimum: 6 }
end

One of the errors seems to be from the user definition, but I cannot find the source of error. 错误之一似乎来自用户定义,但我找不到错误的根源。 Is it something due related to "validates" in user.rb file ? 是否与user.rb文件中的“ validates”有关? Any help will be much appreciated !! 任何帮助都感激不尽 !!

FAIL["test_email_validation_should_accept_valid_addresses", UserTest, 0.061796445]
 test_email_validation_should_accept_valid_addresses#UserTest (0.06s)
        "user@example.com" should be valid
        test/models/user_test.rb:34:in `block (2 levels) in <class:UserTest>'
        test/models/user_test.rb:32:in `each'
        test/models/user_test.rb:32:in `block in <class:UserTest>'

 FAIL["test_should_be_valid", UserTest, 0.080466737]
 test_should_be_valid#UserTest (0.08s)
        Failed assertion, no message given.
        test/models/user_test.rb:8:in `block in <class:UserTest>'

Thanks in advance for your help ! 在此先感谢您的帮助 !

You are right, one of the errors is due to @user being invalid in the first test. 没错,其中一个错误是由于@user在第一次测试中无效。 Do the following in rails console, this should print out errors on user model: 在Rails控制台中执行以下操作,这应该在用户模型上打印出错误:

@user = User.new(name: "Example User", email: "user@example.com", password: "wordpass", password_confirmation: "wordpass")
@user.valid?
pp @user.errors

在user.rb中尝试其他正则表达式

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i

well everything looks fine. 好,一切看起来都很好。 just run the command 只需运行命令

rake db:test:prepare

and if this doesnt work either then 如果这也不起作用,那么

$rake db:delete
rake db:create
rake db:migrate

first one worked for me 第一个为我工作

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

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