2012-09-06 63 views
37

在沒有Rails的RSpec的Ruby中進行TDD的過程是什麼?如何在不使用Rails的情況下使用RSpec?

我需要一個Gemfile嗎?它只需要rspec嗎?

的Ruby 1.9.3

+0

我認爲這幾乎是一樣的沒有,因爲有鐵軌之間並沒有直接聯繫rspec的。 – Cubic

+2

vimeo教程在這裏:http://blog.codeship.com/install-rspec-tutorial/ – Rimian

+0

從頁面上的標題和視頻的標題看起來有點難,但這似乎是Rails和無Rails的Ruby項目。 –

回答

58

的過程如下:

從控制檯安裝RSpec的寶石:

gem install rspec 

然後創建一個文件夾(我們將其命名爲根)以下內容:

根/ my_model.rb

根/規格/ my_m odel_spec.rb

#my_model.rb 
class MyModel 
    def the_truth 
    true 
    end 
end 

#spec/my_model_spec.rb 

require_relative '../my_model' 

describe MyModel do 
    it "should be true" do 
    MyModel.new.the_truth.should be_true 
    end 
end 
在控制檯中運行

rspec spec/my_model_spec.rb 

呢!

39

從項目目錄中...

gem install rspec 
rspec --init 

然後寫規範中創建的規範目錄,並通過

rspec 'path to spec' # or just rspec to run them all 
1

各地gem install rspec的工作流程是有缺陷的運行。始終使用Bundler和Gemfile來確保一致性,並避免項目在一臺計算機上正常工作但在另一臺計算機上失敗的情況。

創建Gemfile

source 'https://rubygems.org/' 

gem 'rspec' 

然後執行:

gem install bundler 
bundle install 
bundle exec rspec --init 

以上將爲您.rspecspec/spec_helpers.rb

現在spec/example_spec.rb創建例如規格:

describe 'ExampleSpec' do 
    it 'is true' do 
    expect(true).to be true 
    end 
end 

並運行規格:

% bundle exec rspec 
. 

Finished in 0.00325 seconds (files took 0.09777 seconds to load) 
1 example, 0 failures 
相關問題