简体   繁体   English

在 Rails 视图中测试客户端验证

[英]Testing for client validations in Rails views

In the Rails application that I am working on I have a page that should display an error message when a field is not populated and the form is submitted.在我正在处理的 Rails 应用程序中,我有一个页面,当未填充字段并提交表单时,该页面应显示错误消息。

The client side validation on the field is done using jQuery and it works fine from the browser , when I test it manually该字段的客户端验证是使用 jQuery 完成的,当我手动测试时,它在浏览器中工作正常

The form is stopped from being submitted at all when data for a field is not entered by the user.当用户未输入字段数据时,表单将完全停止提交。

What are my options to write a test for this我有什么选择来为此编写测试

  1. I looked at Jasmine, but it seems to be more useful when i want to test the javascript function itself , not helpful to test if the event is firing correctly on the page.( let me know if i am wrong on this)我查看了 Jasmine,但是当我想测试 javascript 函数本身时它似乎更有用,对于测试事件是否在页面上正确触发没有帮助。(如果我错了,请告诉我)

  2. I looked at rspec and wrote a test like this我查看了 rspec 并写了一个这样的测试

    require 'spec_helper' describe 'Sample Test Page' do it 'should show error when Some Field is not specified' do visit '/sample_page' click_button "Submit" page.should have_content 'You forgot to Enter details in Some Field' end end

but what is happening here is that the form gets submitted without the validation firing at all and I get an error ( since the application throws an error for blank value)但这里发生的事情是表单在没有验证触发的情况下被提交,我收到一个错误(因为应用程序为空值抛出错误)

What is my best option ?我最好的选择是什么? Is there another tools that will help me test this in the Rails philosophy ?是否有其他工具可以帮助我在 Rails 哲学中对此进行测试?

If you need to test JavaScript in integration tests, you need to explicitly specify it to pick a web driver supporting JavaScript.如果您需要在集成测试中测试 JavaScript,则需要明确指定它以选择支持 JavaScript 的 Web 驱动程序。

In your case you used Webrat, it is fast but does not support JS, so your test failed.在您使用 Webrat 的情况下,它速度很快但不支持 JS,因此您的测试失败了。

The default web driver for Javascript is Selenium. Javascript 的默认 Web 驱动程序是 Selenium。 To enable it, just change this line要启用它,只需更改此行

describe 'Sample Test Page' do 

To

describe 'Sample Test Page', js: true do

Then all tests under this block will run on Selenium.然后这个块下的所有测试都将在 Selenium 上运行。 You'll see a real browser pop up to run your test.你会看到一个真实的浏览器弹出来运行你的测试。

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

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