安裝 RSpec
如果你想使用 RSpec 作為 Rails 專案,你應該使用 rspec-rails gem,它可以自動為你生成幫助器和 spec 檔案(例如,當你使用 rails generate 建立模型,資源或 Scaffolding 時)。
將 rspec-rails 新增到 Gemfile 中的:development 和:test 組:
group :development, :test do
gem 'rspec-rails', '~> 3.5'
end
執行 bundle 以安裝依賴項。
初始化為:
rails generate rspec:install
這將為你的測試建立一個 spec/資料夾,以及以下配置檔案:
.rspec包含命令列rspec工具的預設選項spec/spec_helper.rb包括基本的 RSpec 配置選項spec/rails_helper.rb新增了更具體的配置選項,以便一起使用 RSpec 和 Rails。
所有這些檔案都使用合理的預設值編寫,以幫助你入門,但隨著測試套件的增長,你可以新增功能並更改配置以滿足你的需求。