Rspec 让哈希键意外 =>,期望'}'



我在使用ruby hash键格式和 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
    

相关内容

  • 没有找到相关文章

最新更新