(1)功能測試須要用到Capybara、DatabaseCleaner、Launchy數據庫
(2)Capybara使用例如click_link、fill_in和visit,來模擬用戶在瀏覽器中和應用程序交互的過程瀏覽器
(3)功能測試應該用feature替換掉describe,用scenario替換itruby
例:dom
feature 'User management' do scenario "adds a new user" do admin = create(:admin) visit root_path click_link 'Log In' fill_in 'Email', wirh: admin.email fill_in 'Password', with: admin.password click_button 'Log In' visit root_path expect do click_link 'Users' click_link 'New User' fill_in 'Email', with: 'newuser@example.com' find('#password').fill_in 'Password', with: 'secret123' find('#password_confirmation').fill_in 'Password confirmation', with:‘secret123’ click_button 'Create User' end.to change(User, :count).by(1) expect(current_path).to eq users_path expect(page).to have_content 'New user created' within 'h1' do expect(page).to have_content 'Users' end expect(page).to have_content 'newuser@exaple.com' end end
(4)find('#password')經過dom元素的id查找,咱們還能夠經過XPath路徑查找,或者使用普通的文本查找,例如click_link 'Users',若是沒找到測試會報錯測試
(5)within用來限定查找指定區域,within 'h1'表示只在<h1>標籤中查找ui
(6)功能測試中,一個測試用例或場景中能夠包含多個指望code
(7)Capybara 2.0對DSL句法作了一些該表,用功能測試替換了請求測試,請求測試僅僅用於API接口測試接口
(8)feature和scenario僅用與功能測試,功能測試中用background對應before,given對應let,同時feature不能嵌套事務
(9)Launchy用於把功能測試當前渲染也面保存到一個臨時文件夾,而後在系統默認瀏覽器中打開ip
(10)Launchy使用方法,在想看頁面的地方加入save_and_open_page
(11)Capybara默認的Web啓動是Rack::Test,沒法處理JavaScript,能夠使用Selenium來模擬JavaScript
(12)Selenium使用,只須要在須要js交互的scenario的block前加上js: true就能夠了
(13)使用Selenium的同時,須要是用Database Cleaner處理數據庫事務,爲了將事務清理乾淨
(14)Database Cleaner配置:
spec/spec_helper.rb
RSpec.configure do |config| config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end spec/support/shared_db_connection.db class ActiveRecord::Base mattr_accessor :shared_connection @@shard_connection = nil def self.connection @@shared_connection || retrieve_connection end end ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection