2014-09-18 69 views
0

這是對用戶模型的測試 - 問題在底部,但無法找出錯誤原因,儘管引用了特定行。獲取參數錯誤:參數的錯誤數量(1代表2)

require 'test_helper' 

class UserTest < ActiveSupport::TestCase 

    def setup 
    @user = User.new(name: "Example User", email: "[email protected]") 
    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[[email protected],com user_at_foo.org [email protected]@bar_baz.com [email protected]+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+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
      uniqueness: { case_sensitive: false } 
end 

這是我得到的錯誤,但真的不知道如何,我打錯了arguements的,特別是因爲這是從邁克爾哈特爾書的第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

在'assert_not duplicate_user.valid?'後面似乎有一個額外的'end'。正如下面所指出的那樣,這個'end'屬於之前的測試。 – 2014-09-19 00:25:54

+0

你缺少'test'的最後結束電子郵件驗證應拒絕無效地址「do」 – usha 2014-09-19 00:26:01

+0

感謝您的回覆,但這些建議都不是解決方法 - 我試着結束,結束和結束,結束最後2個塊加上其他組合...只是不要得到它? – user3735114 2014-09-19 00:33:05

回答

0

end s的在最後2個測試混合了一下。以下完整修改後的文件:

require 'test_helper' 

class UserTest < ActiveSupport::TestCase 

    def setup 
    @user = User.new(name: "Example User", email: "[email protected]") 
    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[[email protected],com user_at_foo.org [email protected]@bar_baz.com [email protected]+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 
+0

這正是我所擁有的,我仍然得到相同的錯誤?! – user3735114 2014-09-19 00:49:35

+1

好吧,也許12小時的這讓我有點累了。我現在已經得到了這個解釋並且感謝你爲解釋爲什麼插入'結尾'的原因。這可能是一個令人沮喪的過程! – user3735114 2014-09-19 00:56:57

+0

沒問題。我記得不久之前就開始學習Rails--我知道這種挫敗感,但最終它是值得的。乾杯! – 2014-09-19 00:59:24

相關問題