BDD:
一種直觀的測試應用程序表現的方法,而不關注具體的實現過程,這是 TDD 的一個變種,叫做
BDD(行爲驅動開發,Behavior-driven Development)
生成集成測試
$ rails generate integration_test static_pages
設置rails使用rspec測試而不是test_unit
$ rails generate rspec:install
生成StaticPages 控制器
$ rails generate controller StaticPages home help --no-test-framework
ps : --no-test-framework 選項禁止生成 RSpec 測試代碼
進行測試
$bundle exec rspec spec/requests/static_pages_spec.rb
接下來---->
把 Capybara DSL 加入 RSpec 幫助文件
spec/spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
RSpec.configure do |config|
config.include Capybara::DSL
end
我們有很多種方式來運行測試代碼,3.6 節中還提供了一些便利且高級的方法
然後修改-->
spec/requests/static_pages_spec.rb裏面代碼,添加代碼
示例:
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end