[英]Test BrainTree with rspec and capybara in Rails 4
我正在開發Rails 4應用,我想為BrainTree編寫一些測試:
在Rails 4.0.0中使用rspec-rails(2.14.0)和capybara(2.1.0)
問題在於路線上,在Braintree的表單中,我傳遞了:url
<%= form_for :customer, :url => Braintree::TransparentRedirect.url do |f| %>
現在,當我運行這樣的功能測試時:
it 'should make new payment info' do
login
visit new_customer_path
page.fill_in 'customer_credit_card_number', :with => '4111111111111111'
page.fill_in 'customer_credit_card_expiration_date', :with => '01/25'
page.fill_in 'customer_credit_card_cvv', :with => '400'
page.click_button 'Save Payment Info'
page.should have_content('Payment Info Confirmation')
page.should have_content('411111******1111')
end
我在路線上遇到錯誤:
Failure/Error: page.click_button 'Save Payment Info'
ActionController::RoutingError:
No route matches [POST] "/merchants/fvn6vfc5ptyg2xrp/transparent_redirect_requests"
我還在控制器測試(使用render_views)中嘗試了此方法:
it 'should make new payment info' do
sign_in_as_user
visit new_customer_path
page.fill_in 'customer_credit_card_number', :with => '4111111111111111'
page.fill_in 'customer_credit_card_expiration_date', :with => '01/25'
page.fill_in 'customer_credit_card_cvv', :with => '400'
page.click_button 'Save Payment Info'
save_and_open_page
page.should have_content('Payment Info Confirmation')
page.should have_content('411111******1111')
end
路線上出現相同的錯誤...
在瀏覽器中的開發環境中,它工作正常,我看起來像表單中的:url選項被capybara忽略了嗎? 我想知道有人可以幫我嗎?
我還發現,這些實例應用布倫特里使用Rails: https://github.com/braintree/braintree_ruby_examples/blob/master/rails3_tr_devise/spec/controllers/customer_controller_spec.rb當我運行該項目它的工作原理測試..也許我的問題與Rails和rspec的版本有關?
提前謝謝了!!
實際上,我在《 Rails多租戶》一書中介紹了這個確切的場景。
您的測試與Braintree的項目測試之間的區別在於,您的測試是Capybara的功能,而它們是控制器的規格。
我在書中提到了水豚自述文件的相關部分 :
RackTest是Capybara的默認驅動程序。 它是用純Ruby編寫的,不支持執行JavaScript。 由於RackTest驅動程序直接與Rack接口進行交互,因此不需要啟動服務器。 但是,這意味着如果您的應用程序不是Rack應用程序(Rails,Sinatra和大多數其他Ruby框架是Rack應用程序),則您將無法使用此驅動程序。 此外,您不能使用RackTest驅動程序來測試遠程應用程序或訪問您的應用程序可能與之交互的遠程URL(例如,重定向到外部站點,外部API或OAuth服務)。
解決這個問題的方法是,我編寫了一個名為fake_braintree_redirect
的gem,它在測試環境fake_braintree_redirect
一塊中間件插入請求堆棧中,以捕獲這些請求並進行適當響應。 使用application.rb
定義的initializer
塊將中間件添加到堆棧中,如下所示:
initializer 'middleware.fake_braintree_redirect' do
if Rails.env.test?
require 'fake_braintree_redirect'
config.middleware.use FakeBraintreeRedirect
end
end
這使Braintree完全脫離了方程式,並在每次向其發送數據時返回成功的響應。
另外,如果您確實想對Braintree的沙箱進行測試,則可以通過將scenario
標記為以下內容來切換到JavaScript驅動程序:
scenario "foo", :js => true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.