這是對用戶模型的測試 - 問題在底部,但無法找出錯誤原因,儘管引用了特定行。獲取參數錯誤:參數的錯誤數量(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>'
在'assert_not duplicate_user.valid?'後面似乎有一個額外的'end'。正如下面所指出的那樣,這個'end'屬於之前的測試。 – 2014-09-19 00:25:54
你缺少'test'的最後結束電子郵件驗證應拒絕無效地址「do」 – usha 2014-09-19 00:26:01
感謝您的回覆,但這些建議都不是解決方法 - 我試着結束,結束和結束,結束最後2個塊加上其他組合...只是不要得到它? – user3735114 2014-09-19 00:33:05