2017-04-27 110 views
-1

我混淆了使用紅寶石散列碼格式和letRspec讓散列鍵意外=>,期待'}'

這在正常情況下工作。

{ 
      "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

我認爲,問題是,你需要將你的哈希移到在'..「之後加一個'{}'並移除'''easy'},' –

+0

你的問題是什麼? – sawa

+0

順便說一下,在代碼中甚至沒有''id「=> 3'。 – sawa

回答

2
  1. 如果您的區段跨越超過一行使用do/end
  2. 當上述操作後,您會看到你錯過了兩個開{}哈希:

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