NoMethodError:未定义的方法`call’

运行以下测试套件时:

require 'spec_helper' describe User do before { @user = User.(name: "Example User", email: "user@example.com" } subject { @user } it { should respond_to(:name) } it { should respond_to(:email) } end 

我收到此错误:

 Failure/Error: before { @user = User.(name: "Example User", email: "user@example.com") } NoMethodError: undefined method `call' for # # ./spec/models/user_spec.rb:15:in `block (2 levels) in ' 

创建用户在控制台中工作正常,它响应方法。

您有语法错误:

 before { @user = User.(name: "Example User", email: "user@example.com" } 

应该没有. User和左括号之间。 你也错过了右括号。 尝试:

 before { @user = User.new(name: "Example User", email: "user@example.com") } 

如果你想知道特定的错误消息,在较新的Ruby版本中.()就像call一样:

 l = lambda { |x| x * x } #=> # l.call(3) #=> 9 l.(3) #=> 9