运行测试时,如果尝试使用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