【问题标题】:Test flash message created by number_field ruby on rails测试由 number_field ruby​​ 在 Rails 上创建的 Flash 消息
【发布时间】:2014-01-05 20:22:38
【问题描述】:

试图写一个接受来测试显示的错误消息。我在测试 form_for 中的 number_field 类型时遇到问题。

组 - Index.html.erb

 <div class='row'>
    <div class="small-4 columns">
      <%= f.label :size %>
      <%= f.number_field :size, in: 1..12 %>
    </div>
  </div>

问题在于,如果输入不在 1 到 12 之间,number_field 会阻止表单提交。它会显示一条关于数字必须在 1 到 12 之间的消息。

因为表单根本没有提交,所以我在控制器中的 flash[:error] 没有被击中。我不太确定如何测试这个问题。

【问题讨论】:

    标签: ruby-on-rails capybara form-for


    【解决方案1】:

    使用 Capybara/RSpec,您至少可以编写如下内容:

    visit my_form_path
    fill_in ... # all the other stuff on the form
    select '99', from: 'size'
    click_button 'Submit' #or whatever the submit button is called
    expect(page).to_not have_content(...) #text on page rendered if form submit works
    expect(page).to have_content(...) #text on the form the user should still be on
    

    你是对的,如果浏览器支持 HTML5,你将永远看不到 Flash。 Capybara 支持不同的浏览器引擎,但我对它还不够熟悉,不知道它是否支持任何模拟旧浏览器的引擎。如果发生这种情况,您可以换上旧浏览器,您大概会看到 Flash。

    【讨论】:

    • 以上在 Capybara 不支持 HTML5 的情况下有效,因此它实际上提交了表单。但是从用户的角度来看,验收测试,他们实际上并没有提交表单,所以为后一种水豚提交测试的情况编写测试从用户的角度来看是错误的。
    • 我建议使用 Selenium 驱动程序,但我认为 Firefox 不支持此元素。我想知道你是否可以使用它和一个 polyfill,但如果我的深度在那一点上我就出局了。github.com/Modernizr/Modernizr/wiki/…
    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 2016-05-31
    • 2013-04-11
    • 1970-01-01
    • 2015-03-27
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多