-1

我正在开发一个文本游戏引擎,并且在开发探索模块时遇到了障碍。当我尝试Location使用来自夹具的块参数实例化我的类时,我收到此错误:

ArgumentError,“参数数量错误(3 对 2)

对于此代码:

# in lib/explore/models/location.rb, line 15
class Location < RoundTable::Controllers::ActionDelegate
  # ... code omitted for brevity

  def initialize(slug, params = nil, &block)
    # ... code omitted for brevity

    if block_given?
      parser = Explore::Parsers::LocationParser.new(self)
      parser.instance_eval &block
    end # if
  end # constructor

  # ... code omitted for brevity
end # class Location

# in spec/fixtures/models/locations.rb, line 38
location :mushroom_kingdom, :name => "Mushroom Kingdom" do
  edges = Explore::Fixtures[:edges]
  edges.each do |key, value|
    go value.location, *value.params
  end # each
end # location

# in spec/models/location_spec.rb, line 193
context "initialized with block" do
  let :fixture do fixtures[:mushroom_kingdom] end

  subject { described_class.new fixture.slug, fixture.params, &fixture.block }

  it { puts subject.inspect }
end # context initialized with block

这很明显是某个地方的语法错误,其中一个块没有被转换为一个过程,反之亦然,但我已经检查了十几次代码并且找不到它。如果有眼尖的读者可以帮助我,我将永远感激不尽。

源文件:

完整的源代码在 Github 上可用,但分为两个存储库:

要运行代码或规范,插件代码需要放在 vendor/modules/plugins/explore 的引擎目录中。

回溯:

1) RoundTable::Vendor::Plugins::Explore::Models::Location initialization with block 
     Failure/Error: it { expect { described_class.new fixture.slug, fixture.params, &fixture.block }.not_to raise_error ArgumentError }
       expected no ArgumentError, got #<ArgumentError: wrong number of arguments (3 for 2)>
     # ./spec/models/location_spec.rb:33:in `block (4 levels) in <top (required)>'

2) RoundTable::Vendor::Plugins::Explore::Models::Location initialized with block 
     Failure/Error: subject { described_class.new fixture.slug, fixture.params, &fixture.block }
     ArgumentError:
       wrong number of arguments (3 for 2)
     # ./lib/explore/parsers/location_parser.rb:39:in `go'
     # ./spec/fixtures/models/locations.rb:41:in `block (2 levels) in <module:Models>'
     # ./spec/fixtures/models/locations.rb:40:in `each'
     # ./spec/fixtures/models/locations.rb:40:in `block in <module:Models>'
     # ./lib/explore/models/location.rb:49:in `instance_eval'
     # ./lib/explore/models/location.rb:49:in `initialize'
     # ./spec/models/location_spec.rb:196:in `new'
     # ./spec/models/location_spec.rb:196:in `block (3 levels) in <top (required)>'
     # ./spec/models/location_spec.rb:198:in `block (3 levels) in <top (required)>'

编辑 2012 年 06 月 06 日:

  • 用实际的、不合格的代码替换了示例代码
  • 添加了完整项目的链接
4

3 回答 3

2

rspec 有时会以不完全有帮助的方式吞下回溯(我认为您可以使用 -b 选项来抑制这种情况) - 错误(我认为)在其他地方。

根据我对代码的阅读,蘑菇王国的块是 instance_evaled 在一个实例上Explore::Parsers::LocationParser

这个类定义了一个go这样的方法

def go(location, params = {})

但蘑菇王国有

go value.location, *value.params

哪里value.params是用于在边缘夹具文件底部创建边缘的选项哈希。在至少一种情况下,该散列中有 2 个对象,因此当您发出 splat 时,您最终会go使用 3 个参数进行调用。鉴于这go似乎将哈希作为其第二个参数,那 splat 真的是故意的吗?

于 2012-06-07T00:06:25.913 回答
0

尝试:

Location.new(fixture[:slug], fixture[:params], fixture[:block])
于 2012-06-06T17:31:12.440 回答
0

这是您拥有的东西的复制品,并且可以正常工作。仍在寻找问题。

class Location
   def initialize(slug, params = nil, &block)
     yield
   end
end


mushroom_kingdom = 'mushroom kingdom'
fixture = {
  :slug => mushroom_kingdom,
  :params => { :princess => :toadstool },
  :block => lambda { puts "It'sa me, Mario!" }
}
l = Location.new(fixture[:slug], fixture[:params],&fixture[:block])

为了得到同样的错误,我必须做Location.new(fixture[:slug], fixture[:params], 'abcd', &fixture[:block])or Location.new(fixture[:slug], fixture[:params],fixture[:block])。几乎就好像您&的不是真正的&符号。是其他字符编码吗?前两个参数中是否有逗号?

而且,您确定错误正是来自此代码吗?您共享的内容没有语法错误。

于 2012-06-06T17:33:18.883 回答