简体   繁体   English

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

[英]Test BrainTree with rspec and capybara in Rails 4

I'm working on a Rails 4 app and i want to write some tests for BrainTree: 我正在开发Rails 4应用,我想为BrainTree编写一些测试:

using rspec-rails (2.14.0) and capybara (2.1.0) in Rails 4.0.0 在Rails 4.0.0中使用rspec-rails(2.14.0)和capybara(2.1.0)

The problem is with on the route, in the form for Braintree i pass a :url 问题在于路线上,在Braintree的表单中,我传递了:url

<%= form_for :customer, :url => Braintree::TransparentRedirect.url do |f| %>

Now when i run a feature test like this one: 现在,当我运行这样的功能测试时:

  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

i get a error on the route: 我在路线上遇到错误:

    Failure/Error: page.click_button 'Save Payment Info'
 ActionController::RoutingError:
   No route matches [POST] "/merchants/fvn6vfc5ptyg2xrp/transparent_redirect_requests"

I've also tried this in a controller test (with render_views): 我还在控制器测试(使用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

Same error on the route... 路线上出现相同的错误...

In development env in the browser it works fine, i looks like the :url option in my form gets ignored by capybara? 在浏览器中的开发环境中,它工作正常,我看起来像表单中的:url选项被capybara忽略了吗? I wonder if anybody can help me with this? 我想知道有人可以帮我吗?

I've also found these examples apps for Braintree with Rails: https://github.com/braintree/braintree_ruby_examples/blob/master/rails3_tr_devise/spec/controllers/customer_controller_spec.rb when i run the tests on that project it works.. maybe my problem has to do with the version of Rails and rspec? 我还发现,这些实例应用布伦特里使用Rails: https://github.com/braintree/braintree_ruby_examples/blob/master/rails3_tr_devise/spec/controllers/customer_controller_spec.rb当我运行该项目它的工作原理测试..也许我的问题与Rails和rspec的版本有关?

many thanks in advance!! 提前谢谢了!!

I actually cover this exact scenario in my Multitenancy with Rails book. 实际上,我在《 Rails多租户》一书中介绍了这个确切的场景。

The difference between your tests and Braintree's project's tests are that your tests are Capybara features and theirs' are controller specs. 您的测试与Braintree的项目测试之间的区别在于,您的测试是Capybara的功​​能,而它们是控制器的规格。

I mention this relevant part of Capybara's README within the book: 我在书中提到了水豚自述文件的相关部分

RackTest is Capybara's default driver. RackTest是Capybara的默认驱动程序。 It is written in pure Ruby and does not have any support for executing JavaScript. 它是用纯Ruby编写的,不支持执行JavaScript。 Since the RackTest driver interacts directly with Rack interfaces, it does not require a server to be started. 由于RackTest驱动程序直接与Rack接口进行交互,因此不需要启动服务器。 However, this means that if your application is not a Rack application (Rails, Sinatra and most other Ruby frameworks are Rack applications) then you cannot use this driver. 但是,这意味着如果您的应用程序不是Rack应用程序(Rails,Sinatra和大多数其他Ruby框架是Rack应用程序),则您将无法使用此驱动程序。 Furthermore, you cannot use the RackTest driver to test a remote application, or to access remote URLs (eg, redirects to external sites, external APIs, or OAuth services) that your application might interact with. 此外,您不能使用RackTest驱动程序来测试远程应用程序或访问您的应用程序可能与之交互的远程URL(例如,重定向到外部站点,外部API或OAuth服务)。

The way I get around it is that I wrote a gem called fake_braintree_redirect which inserts a piece of middleware into the request stack during the test environment to capture these requests and respond appropriately. 解决这个问题的方法是,我编写了一个名为fake_braintree_redirect的gem,它在测试环境fake_braintree_redirect一块中间件插入请求堆栈中,以捕获这些请求并进行适当响应。 The middleware is added to the stack using an initializer block defined within application.rb , like this: 使用application.rb定义的initializer块将中间件添加到堆栈中,如下所示:

initializer 'middleware.fake_braintree_redirect' do
  if Rails.env.test?
    require 'fake_braintree_redirect'
    config.middleware.use FakeBraintreeRedirect
  end
end

This takes Braintree out of the equation completely and returns a successful response whenever you send data to it. 这使Braintree完全脱离了方程式,并在每次向其发送数据时返回成功的响应。


Alternatively, if you really want to test against Braintree's sandbox, you could switch to the JavaScript driver by tagging your scenario as: 另外,如果您确实想对Braintree的沙箱进行测试,则可以通过将scenario标记为以下内容来切换到JavaScript驱动程序:

 scenario "foo", :js => true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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