FactoryGirl不接受财产覆盖

时间:2013-01-12 02:14:44

标签: ruby-on-rails rspec factory-bot

我确信这是一个微不足道的错字,但我没有看到。这段代码是在减少到最小值之后,我仍然看到这种语法错误。原谅我的愚蠢,因为我知道一旦你告诉我我没有看到的东西,我会感觉到的!

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"})

1 个答案:

答案 0 :(得分: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。 空格的替代方法是离开parens。

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

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

[7] pry(main)> (:user, {:name => "a"})
SyntaxError: unexpected ',', expecting ')'
(:user, {:name => "a"})
       ^