2013-10-05 56 views
0

我正试图在rspec中为我正在写的一个gem实现一些简单的测试。当我将describe BikeShare do注释掉到end并运行该文件时,该文件将加载并成功运行。我相信这是我错过的小事。未初始化的常量BikeShare(NameError)

我的测试文件是非常简单的,看起来像这样:

require 'spec_helper' 

describe BikeShare do 

    it "should run" do 
    # response = BikeShare.new 
    # response.should_be present 
    end 

end 

运行时,我得到的错误uninitialized constant BikeShare (NameError)在第3行

bikeshare.rb文件看起来像这样,很简单:

class BikeShare 

    def initialize 
    response = JSON.parse(open("http://bayareabikeshare.com/stations/json").read) 
    @response = response["stationBeanList"] 
    end 

    def get_last_station 
    @response.last["id"] 
    end 
end 

Rakefile看起来是这样的:

require 'rubygems' 
require 'bundler' 
Bundler.setup 

Bundler::GemHelper.install_tasks 

require 'rspec/core/rake_task' 
RSpec::Core::RakeTask.new do |spec| 
    # spec.libs << 'lib' << 'spec' 
    spec.pattern = 'spec/*_spec.rb' 
end 

task :default => :spec 
+0

我不使用rspec,所以我可能会错,但它是如何知道你的自行车共享类?你可能只需要它。 –

+0

工作。谢谢!如果你想写一个答案,很高兴给你信用。 –

回答

2

您的测试不知道BikeShare。

您需要定义BikeShare类的文件。我不使用rspec,但我认为你通常在spec_helper.rb中设置测试环境。

+0

要么在spec_helper.rb中包含bikeshare.rb,要么在bikeshare.spec中包含bikeshare.rb,常见约定是在spec_helper中执行此操作。 – JamesSwift

相关问题