運行測試時,如果嘗試使用User.new
創建新對象,則出現錯誤。如果相反,我使用User.new({})
,它工作正常。ActiveModel ::初始化沒有參數的類時出現的模型錯誤
如果沒有傳入,params是否應該默認爲空?
$ rails -v
Rails 5.0.0.1
user.rb
class User
include ActiveModel::Model
attr_accessor :name, :email, :country
end
user_test.rb
require 'test_helper'
class User < ActiveSupport::TestCase
test "should create an empty user when initialized with no params" do
user = User.new
assert_not_nil(user, 'user cannot be empty')
end
end
測試結果
Error:
User#test_should_create_an_empty_user_when_initialized_with_no_parameters:
ArgumentError: wrong number of arguments (given 0, expected 1)
test/models/user_test.rb:7:in `new'
test/models/user_test.rb:7:in `block in <class:User>'
當您在Rail控制檯中執行User.new時會發生什麼?你在user.rb中有初始化方法嗎? – margo
你確定'test'中的'User'是用戶*模型*而不是用戶*測試*? –
有時,你只需要有人指出明顯的,謝謝@DaveNewton – pameck