gpt4 book ai didi

ruby-on-rails - 无方法错误 : undefined method `call'

转载 作者:数据小太阳 更新时间:2023-10-29 07:29:06 25 4
gpt4 key购买 nike

运行以下测试套件时:

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

创建用户在控制台中工作得很好并且它响应方法。

最佳答案

语法错误:

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

关于ruby-on-rails - 无方法错误 : undefined method `call' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12024908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com