2015-05-16 78 views
0

我正在使用軌道上的紅寶石,我在運行「ruby -Itest測試/模型/ user_test.rb」時遇到問題。當我在正則表達式/^[a-zA-Z0-9-]+$/中進行更改時,出現錯誤,現在我陷入了這個問題,我不知道如何克服它。測試錯誤: - 在軌道上的紅寶石

我user.rb文件: -

validates :profilename, presence: true, 

uniqueness: true, format: { with: /^[a-zA-Z0-9-]+$/ , multiline: true, 

message: 'Must be formatted correctly.' } 

運行試驗: -

$ ruby -Itest test/models/user_test.rb 

DL is deprecated, please use Fiddle 

Run options: --seed 50723 

Running: 
.....#<ActiveModel::Errors:0x4ecc8a0 @base=#<User id: nil, first_name: "sarah", last_name: "gupta", profile_name: "S_arah-1", email: "[email protected] 
il.com", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at 
: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil>, @messages={}> 
E.. 


Finished in 0.380267s, 15.7784 runs/s, 28.9270 assertions/s.. 

1) Error: UserTest#test_a_user_can_have_a_correctly_formatted_profile_name: ArgumentError: wrong number of arguments (0 for 1..2) test/models/user_test.rb:41:in `block in <class:UserTest>' 

6 runs, 11 assertions, 0 failures, 1 errors, 0 skips 

這裏是a_user_can_have_a_correctly_formatted_profile_name

test "a user can have a correctly formatted profile name" do 
    user = User.new(first_name: 'sarah', last_name: 'gupta', email: '[email protected]') 
    user.password = user.password_confirmation = 'welcome' 
    user.profile_name = 's_arah-1' 
    assert.user.valid? 
end 

感謝您的幫助的代碼!

+1

你可以把代碼測試名稱** ** test_a_user_can_have_a_correctly_formatted_profile_name代碼 – Abhi

+0

- 測試「用戶可以有一個正確格式的配置文件名稱」做 用戶= User.new(FIRST_NAME:「莎拉」,姓氏:「古普塔',email:'[email protected]') user.password = user.password_confirmation ='welcome' user.profile_name ='s_arah-1' assert.user.valid? 結束 –

+0

檢查它是'profilename'還是'profile_name'。另外,goto ** test/models/user_test.rb第41行**並檢查正在調用的方法。您可能不會傳遞一些預期的參數 – Abhi

回答

0

它看起來像你的問題的答案在文件user_test.rb。嘗試檢查定義的test_a_user_can_have_a_correctly_formatted_profile_name以及它的調用位置:根據錯誤,它被調用時沒有所需的一個或兩個參數。我猜你需要給它一個測試用戶名。

+0

此代碼「assert!user.valid?」爲我工作,並通過了測試,沒有錯誤。但我不知道這段代碼究竟意味着什麼。? –

+0

'!'表示「不」,所以如果'assert!user.valid?'通過,這意味着用戶無效。 – rainbowsorbet