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

时间:2017-04-27 08:44:39

标签: ruby-on-rails ruby rspec let

我在使用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 个答案:

答案 0 :(得分:2)

  1. 如果您的区块跨越多个线路,请使用do / end
  2. 完成上述操作后,您会发现您错过了哈希的开头{和结束}

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