我有这段代码:
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",
答案 0 :(得分: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">
或者使用params / block的混合模式:
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">
在这个阶段,我猜这个问题在某处是一个未封闭的区块。
答案 1 :(得分: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 })