2016-07-22 159 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函数。

相关问题