0

我有这个代码:

 19   context 'overriding at fabricate time' do
 20     let(:fabricated_object) do
 21       Fabricate(
 22         "#{fabricator_name}_with_children", do
 23         string_field: 'new content',
 24         number_field: 10,
 25         nil_field: nil,
 26         placeholder: 'is not invoked',
 27         dynamic_field: { 'new dynamic content' }
 28       end)
 29     end

制造商:

  1 class Fabrication::Fabricator
  2 
  3   def self.build(name, overrides={}, &block)
  4     fail_if_initializing(name)
  5     schematic(name).build(overrides, &block)
  6   end
  7 
  8   def self.fabricate(name, overrides={}, &block)
  9     fail_if_initializing(name)
 10     schematic(name).fabricate(overrides, &block)
 11   end
 12 
 13   def self.to_attributes(name, overrides={}, &block)
 14     fail_if_initializing(name)
 15     schematic(name).to_attributes(overrides, &block)
 16   end
 17 
 18   def self.to_params(name, overrides={}, &block)
 19     fail_if_initializing(name)
 20     schematic(name).to_params(overrides, &block)
 21   end
 22 
 23   private
 24 
 25   def self.fail_if_initializing(name)
 26     raise Fabrication::MisplacedFabricateError.new(name) if Fabrication.manager.initializing?
 27   end
 28 
 29   def self.schematic(name)
 30     Fabrication::Support.find_definitions if Fabrication.manager.empty?
 31     Fabrication.manager[name] || raise(Fabrication::UnknownFabricatorError.new(name))
 32   end
 33 
 34 end

但我不断得到

integration_spec.rb:23: syntax error, unexpected ':', expecting ')' (SyntaxError)
        string_field: 'new content',
                     ^
/home/durrantm/Dropbox/96_2013/work/code/ruby/fabrication_temp/spec/_seded/integration_spec.rb:23: syntax error, unexpected ',', expecting kEND
/home/durrantm/Dropbox/96_2013/work/code/ruby/fabrication_temp/spec/_seded/integration_spec.rb:25: Can't assign to nil
/home/durrantm/Dropbox/96_2013/work/code/ruby/fabrication_temp/spec/_seded/integration_spec.rb:26: syntax error, unexpected ':', expecting '='
        placeholder: 'is not invoked'
                    ^
/home/durrantm/Dropbox/96_2013/work/code/ruby/fabrication_temp/spec/_seded/integration_spec.rb:27: syntax error, unexpected ')', expecting kEND
...

我怎样才能解决这个问题?我已经尝试以多种方式格式化哈希,但都没有奏效。

曾经以这种格式工作的代码(我试图期待):

  context 'overriding at fabricate time' do
    let(:fabricated_object) do
      Fabricate(
        "#{fabricator_name}_with_children",
        string_field: 'new content',
        number_field: 10,
        nil_field: nil,
        placeholder: 'is not invoked'
      ) do
        dynamic_field { 'new dynamic content' }
      end
    end

我也试过:

 19   context 'overriding at fabricate time' do
 20     let(:fabricated_object) do
 21       Fabricate(
 22         "#{fabricator_name}_with_children",
 23         { string_field: "new content",
 24         number_field: 10,
 25         nil_field: nil,
 26         placeholder: 'is not invoked',
 27         dynamic_field: { 'new dynamic content' }})
 28       
 29     end

但得到了

integration_spec.rb:23: odd number list for Hash (SyntaxError)
    { string_field: "new content",
4

2 回答 2

1

尝试将调用安排Fabricate(...)如下:

  Fabricate("#{fabricator_name}_with_children".to_sym) do
    string_field  'new content'
    number_field  10 
    nil_field     nil
    placeholder   'is not invoked'
    dynamic_field { 'new dynamic content' }
  end 

编辑:我做了一些本地控制台实验以确保它应该工作。我假设这些值是制造商的默认值,例如

2.0.0p195 :014 > Fabricator(:x) do
2.0.0p195 :015 >   string_field  'new content'
2.0.0p195 :016?>   number_field  10 
2.0.0p195 :017?>   nil_field     nil
2.0.0p195 :018?>   placeholder   'is not invoked'
2.0.0p195 :019?>   dynamic_field { 'new dynamic content' }
2.0.0p195 :020?> end

我可以制造一个实例 ok--

2.0.0p195 :032 > Fabricate(:x)
 => #<X:0x98c32c4 @string_field="new content", @number_field=10, @nil_field=nil, @placeholder="is not invoked", @dynamic_field="new dynamic content"> 

我可以像上面那样通过一个街区——

2.0.0p195 :055 > Fabricate(:x) do
2.0.0p195 :056 >   string_field  'alternate content'
2.0.0p195 :057?>   number_field  999
2.0.0p195 :058?>   nil_field     nil
2.0.0p195 :059?>   placeholder   'changed'
2.0.0p195 :060?>   dynamic_field { 'different dynamic content' }
2.0.0p195 :061?> end 
 => #<X:0x948eff0 @string_field="alternate content", @number_field=999, @nil_field=nil, @placeholder="changed", @dynamic_field="different dynamic content"> 

或者使用带有参数/块的混合模式:

2.0.0p195 :062 > Fabricate(:x, string_field: 'new content', number_field: 37, nil_field: nil, placeholder: 'blah') do
2.0.0p195 :063 >       dynamic_field { 'new dynamic content' }
2.0.0p195 :064?>   end 
 => #<X:0x92c4e54 @string_field="new content", @number_field=37, @nil_field=nil, @placeholder="blah", @dynamic_field="new dynamic content">

在这个阶段,我猜问题是某个地方的未封闭块。

于 2013-10-27T19:40:24.503 回答
1

这不应该:

 21       Fabricate(
 22         "#{fabricator_name}_with_children", do
 23         # ...
 28       end)

更像:

 21       Fabricate(
 22         "#{fabricator_name}_with_children") do
 23         # ...
 28       end

如在do之前使用右括号,而不是(错误地)包装它?

另一种可能的选择是,如果它仍然吐出错误,以确保正确应用运算符优先级:

 21       Fabricate(
 22         "#{fabricator_name}_with_children") {
 23         # ...
 28       }

还有另一种选择,看到那个东西的内容看起来像一个哈希而不是一个块:

 21       Fabricate(
 22         "#{fabricator_name}_with_children", {
 23         # ...
 28       })
于 2013-10-27T20:16:43.897 回答