NoMethodError:未定义的方法“调用" [英] NoMethodError: undefined method `call'

查看:47
本文介绍了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

这篇关于NoMethodError:未定义的方法“调用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆