意外=>,期待在rspec期待’}’

我有这个代码:

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} 

我错过了什么错误或错误的代码给了我错误? 这是一个常见的问题吗?

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

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

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

例如

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