【问题标题】:unexpected =>, expecting '}' in rspec expect意外 =>,在 rspec 中期待“}”
【发布时间】:2013-11-02 13:54:08
【问题描述】:

我有这个代码:

adapter.send(:conditions_to_fields, :user => user).should == {'owner_id' => user.id}

我更新为使用期望:

expect(adapter.send(:conditions_to_fields, :user => user)).to eq {'owner_id' => user.id}

但这给了我:

syntax error, unexpected =>, expecting '}'
... => user)).to eq {'owner_id' => user.id}

我的代码有什么遗漏或错误导致该错误? 这是一个普遍的问题吗?

【问题讨论】:

  • 为什么在.should matcher 看起来更干净且有效时使用“expect”? :S
  • @arieljuod 因为should 不是首选语法,所以expect 是。请参阅RSpec’s New Expectation Syntax(由 RSpec 的一位核心贡献者提供)。

标签: ruby rspec


【解决方案1】:

它认为你将一个块传递给eq,而不是一个哈希。您可以通过在方法参数周围使用括号来解决此问题:

expect(adapter.foo).to eq({'owner_id' => user.id})

【讨论】:

【解决方案2】:

解决这个问题的一种方法是使用Hash 而不是{}

例如

expect(adapter.send(:conditions_to_fields, :pwner => user)).to eq \
Hash('owner_id' => user.id, 'owner_type' => user.class.name)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2011-09-23
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2019-12-06
    相关资源
    最近更新 更多