5

运行以下测试套件时:

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

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

4

1 回答 1

7

你有一个语法错误:

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
于 2012-08-19T07:24:32.583 回答