【问题标题】:NoMethodError: undefined method `call'NoMethodError:未定义的方法“调用”
【发布时间】:2012-08-19 07:19:08
【问题描述】:

运行以下测试套件时:

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)>'

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

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    你有一个语法错误:

    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 }
    #=> #<Proc:0x007fe5d3907188@(pry):39 (lambda)>
    l.call(3)
    #=> 9
    l.(3)
    #=> 9
    

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2019-06-12
      • 2013-11-14
      • 2021-07-07
      相关资源
      最近更新 更多