【问题标题】:Rails 5/rspec/capybara: How do I write a feature request for an AJAX form?Rails 5/rspec/capybara:如何为 AJAX 表单编写功能请求?
【发布时间】:2019-07-31 17:15:19
【问题描述】:

我有一个要测试的远程表单。

我的功能测试如下所示:

RSpec.feature 'User creates a foobar' do
  scenario 'they see the foobar on the page' do
    get '/apply', :js

    fill_in 'Name', with: 'Joe Bloggs'

    click_button 'Submit application'
    #...

但是,click_button 'Submit application' 因错误而中断:

*** ActionController::UnknownFormat Exception: ActionController::UnknownFormat

我的控制器只在 JS 中响应。没有重定向/渲染。我想最终检查数据库中是否存在记录。

当响应为js 时,我需要做些什么来确保它不会中断?

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    您需要使用支持 JS 的驱动程序 - https://github.com/teamcapybara/capybara#drivers 运行 - 在默认设置中,它可以像指定驱动程序一样简单(Capybara.javascript_driver = :selenium_chrome 用于 chrome 通过 selenium 等)并设置 js: true 元数据为测试。另请注意,在使用 Capybara 时,您不能使用直接请求方法(getpost 等) - 您必须告诉它改为访问页面。

    RSpec.feature 'User creates a foobar', js: true do # `js: true` applies to all scenarios in this feature - could be put on single scenario instead if desired
      scenario 'they see the foobar on the page' do  
        visit '/apply'
    
        fill_in 'Name', with: 'Joe Bloggs'
    
        click_button 'Submit application'
        #...
    

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多