如何将transient的默认值设置为hash?

时间:2015-09-11 11:45:57

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

我尝试使用transient属性创建工厂,当我将其默认值设置为hash时,我遇到了一些语法错误:

FactoryGirl.define do
  factory :event do
    transient do 
      location {city: nil, state: nil, country: "Polska"}
    end

    name "MyString"
    start_date Date.today+2
    start_time "17:40"
    location_raw "MyLocation"
    description "MyText"
    website "MyString"


    after(:create) do |event, evaluator|
      Location.find_or_create_by(evaluator.location).events << event
    end
  end
end

当我将其设置为nil时,它可以正常工作。

transient do 
  location nil
end

我做错了什么?

错误是:

/home/marcin/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load': /home/marcin/Projects/StreetballKalendarz.pl/spec/factories/events.rb:4: syntax error, unexpected ':', expecting '}' (SyntaxError)
      location {city: nil, state: nil, country: "Polska"}
                     ^
/home/marcin/Projects/StreetballKalendarz.pl/spec/factories/events.rb:4: Can't assign to nil
      location {city: nil, state: nil, country: "Polska"}
                          ^
/home/marcin/Projects/StreetballKalendarz.pl/spec/factories/events.rb:4: Can't assign to nil
      location {city: nil, state: nil, country: "Polska"}

1 个答案:

答案 0 :(得分:2)

工厂女孩在大括号之间进行评估。所以我认为你需要第二套让它理解它是一个哈希

location { { city: nil, state: nil, country: "Polska" } }