0

I am confusing in using ruby hash key format and let.

This works in normal case.

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

but fails in let block

Here's the code:

describe 'incorrect_question' do

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

It results in the following exception:

syntax error, unexpected =>, expecting '}' (SyntaxError)
                  "id" => 1,
                         ^
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
rj487
  • 4,476
  • 6
  • 47
  • 88

1 Answers1

3
  1. If your block spans for more than one line use do/end.
  2. When above is done you'll see that you're missing both the opening { and closing } of the hash:

    let(:wrong_question1) do
      {
                 "id" => 1,
        "description" => "test 3",
        "difficulty" => { "id" => 1, "description" => "easy" }
      }
    end
    
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145