获取ArgumentError:参数数量错误(1表示2)

时间:2014-09-18 23:46:37

标签: ruby-on-rails

这是对用户模型的测试 - 问题在底部,但无法解决为什么它出错了,尽管引用了特定的行。

    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

  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 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?
    end

   #this is the line 36 its complaining about... 
  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
  end  
end

我的/models/user.rb如下:

class User < ActiveRecord::Base
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 }
end

这是我得到的错误,但实际上无法解决我如何获得错误的争论数量,特别是因为这是从迈克尔·哈特斯的第6章书中复制的 - 已经在网上搜索了大约一天现在,我们可以简单地解决这个问题吗?

ERROR["test_email_validation_should_reject_invalid_addresses", UserTest, 0.0499
93779]                                                                         
 test_email_validation_should_reject_invalid_addresses#UserTest (0.05s)        
ArgumentError:         ArgumentError: wrong number of arguments (1 for 2)      
            test/models/user_test.rb:36:in `test'                              
            test/models/user_test.rb:36:in `block in <class:UserTest>'         
        test/models/user_test.rb:36:in `test'                                  
        test/models/user_test.rb:36:in `block in <class:UserTest>'     

1 个答案:

答案 0 :(得分:0)

您的end在最近的两次测试中有点混淆。完整修订的文件如下:

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

  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 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?
    end  # end of each block
  end    # end of test block

  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    # end of test block
end      # end of class