繁体   English   中英

在Rails 4中使用rspec和capybara测试BrainTree

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM