Search code examples
ruby-on-railsrubyrspeclet

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


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,
                         ^

Solution

    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