0

我确信这是一个微不足道的错字,但我没有看到。这段代码是在削减到最低限度之后,我仍然看到这个语法错误。原谅我的愚蠢,因为我知道,一旦你向我展示我没有看到的东西,我就会感觉到!

FactoryGirl.define do
  factory :user do
    name "a name"
  end
end

@goodguy = FactoryGirl.create (:user, {:name => "a"})

给我:

/Users/pitosalas/.rvm/gems/ruby-1.9.3-p194@repeatsurvey/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load': /Volumes/MonsterHD/mydev/repsurv/spec/models/access_control_spec.rb:29: syntax error, unexpected ',', expecting ')' (SyntaxError)
      @goodguy = FactoryGirl.create (:user, {:name => "a"})
                                           ^
/Volumes/MonsterHD/mydev/repsurv/spec/models/access_control_spec.rb:29: syntax error, unexpected '}', expecting keyword_end
      @goodguy = FactoryGirl.create (:user, {:name => "a"})
4

1 回答 1

3

摆脱之后的空间create

@goodguy = FactoryGirl.create(:user, {:name => "a"})

例如

[3] pry(main)> FactoryGirl.create (:staff_user, {:first_name => "ben"})
SyntaxError: unexpected ',', expecting ')'
FactoryGirl.build (:staff_user, {:first_name => "ben"})
                               ^
[3] pry(main)> FactoryGirl.create(:staff_user, {:first_name => "ben"})
=> #<User::Staff id: 1, ...

解释一下,当您在方法名称后放置一个空格时,方法调用中的所有内容都将被视为参数 #1(...)空格一起使用的替代方法是省略括号。

@goodguy = FactoryGirl.create :user, {:name => "a"}

你会得到一个错误,因为 Ruby 试图将其(:user, {:name => "a"})作为一个表达式进行评估,这当然没有任何意义。

[7] pry(main)> (:user, {:name => "a"})
SyntaxError: unexpected ',', expecting ')'
(:user, {:name => "a"})
       ^
于 2013-01-12T02:21:15.280 回答