NoMethodError:未定义的方法“调用"

NoMethodError:未定义的方法“调用

问题描述:

运行以下测试套件时:

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 #<Class:0x007fdfd5dd8008>
  # ./spec/models/user_spec.rb:15:in `block (2 levels) in <top (required)>'

在控制台中创建用户工作正常并且它响应方法.

Creating the user works in the console just fine and it responds to the methods.

您有一个语法错误:

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

User 和左括号之间不应该有 ..你也缺少右括号.试试:

There should be no . between User and the opening parenthesis. Also you are missing the closing parenthesis. Try:

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

如果您想知道具体的错误信息,在较新的 Ruby 版本中 .() 的工作方式类似于 call:

If you wonder about the specific error message, in newer Ruby versions .() works like call:

l = lambda { |x| x * x }
#=> #<Proc:0x007fe5d3907188@(pry):39 (lambda)>
l.call(3)
#=> 9
l.(3)
#=> 9