2016-07-22 107 views
0

我在Ruby中創建一個directed_graph類來練習使用RSpec。我不斷收到上面的錯誤(在第13行,下面一行是「eql(0)」)。遇到:語法錯誤,意外tidENTIFIER,期待keyword_end

我真的不明白這個錯誤,特別是因爲這個RSpec代碼看起來與我爲其他項目編寫的其他RSpec代碼非常相似。

require "directed_graph" 
include directed_graph 

describe directed_graph do 

    describe ".vertices" do 
     context "given an empty graph" do 
      it "returns an empty hash" do 
       g = directed_graph.new() 
       expect(g.vertices().length()).to() eql(0) 
      end 
     end 
    end 

end 

編輯:我認爲這個問題是(1)directed_graph是一個類,類必須用大寫字母開始(所以我改名爲將DirectedGraph),和(2)你不應該寫「包括「上課。

我修正了這兩個問題,現在我的代碼似乎很好地運行。如果我錯過了一些大事,我會把它留在這裏。

回答

0

我相信代碼應該是這樣的:

require "directed_graph" 
include DirectedGraph 

describe DirectedGraph do 
    describe ".vertices" do 
    context "given an empty graph" do 
     it "returns an empty hash" do 
     expect(directed_graph.new.vertices.length).to eql(0) 
     end 
    end 
    end 
end 

讓我解釋一下爲什麼。首先包括通常包括類/模塊。紅寶石中的類和模塊用大寫字母表示它們名稱的每個部分(也稱爲UpperCamelCase)。當你在rspec中描述一個類時,你也應該使用UpperCamelCase。我還清理了一些代碼,以便於閱讀。你並不總是需要()來表示一個功能。這是隱含的。但有時你確實需要它,例如expect函數。

相關問題