Rspec 让散列键意外 =>,期待“}”

Rspec let hash key unexpected =>, expecting '}'

我在使用 ruby 哈希键格式和 let 时感到困惑。

这在正常情况下有效。

{
           "id" => 1,
  "description" => "test 3",
   "difficulty" => { "id" => 1, "description" => "easy" },
}

但在 let

中失败

代码如下:

describe 'incorrect_question' do

  let(:wrong_question1) {
             "id" => 1,
    "description" => "test 3",
     "difficulty" => { "id" => 1, "description" => "easy" },
  }
  it 'does something' do
    # ...
  end
end

它导致以下异常:

syntax error, unexpected =>, expecting '}' (SyntaxError)
                  "id" => 1,
                         ^
  1. 如果您的块跨越多行,请使用 do/end
  2. 完成上述操作后,您会发现缺少散列的开头 { 和结尾 }

    let(:wrong_question1) do
      {
                 "id" => 1,
        "description" => "test 3",
        "difficulty" => { "id" => 1, "description" => "easy" }
      }
    end