2013-10-05 121 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

相關問題