Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在FactoryGirl中创建字典作为属性_Ruby On Rails_Testing_Rspec_Factory Bot - Fatal编程技术网

Ruby on rails 在FactoryGirl中创建字典作为属性

Ruby on rails 在FactoryGirl中创建字典作为属性,ruby-on-rails,testing,rspec,factory-bot,Ruby On Rails,Testing,Rspec,Factory Bot,Factory Girl允许执行以下操作: FactoryGirl define factory :post do content "some content" styles "styles here" team 1 end end 但是,如果我在factory模块内尝试以下内容: factory :post do content "some content" styles "styles here" team

Factory Girl允许执行以下操作:

FactoryGirl define 
  factory :post do
    content  "some content"
    styles   "styles here"
    team     1
  end
end
但是,如果我在factory模块内尝试以下内容:

  factory :post do
    content  "some content"
    styles   "styles here"
    team     1
    my_dictionary {'a' => 1, 'b' => 2}
  end

my_字典不会被解释为字典类型。我不知道如何在FactoryGirl中制作字典作为属性。有人能帮我吗

您观察到的问题来自Ruby中的语法歧义。该语言使用大括号来定义散列(您称之为字典)和块(例如,当使用
每个
循环时)。由于您现在使用hash作为
my_dictionary
方法的唯一参数,解析器不清楚是否将打开的大括号解释为块的开头或hash。在本例中,Ruby默认为块假设

要将解释作为方法参数强制执行,可以使用如下括号:

my_dictionary({'a' => 1, 'b' => 2})
然后,可以解析该语句而不产生任何歧义。这里介绍的只是少数几种方法调用不能轻易省略括号的情况之一