用水豚进行Jquery验证

时间:2015-12-01 19:04:45

标签: ruby-on-rails rspec jquery-validate capybara

如何使用带有rspec的capybara使用jquery验证编写设计登录表单的测试?

我正在使用rails 4.2.4&水豚2.5.0。 我正在使用jquery验证验证登录表单。 我需要测试我是否将电子邮件或密码字段留空,它应该抛出错误。 我的示例代码是

scenario 'throws error on blank email id' do
  visit '/users/sign_in'
  within('.form-fields') do
    fill_in 'user[email]', with: ''
    fill_in 'user[password]', with: 'abcdefgh'
  end

  click_button 'Log In'
  expect(page).to have_content 'Please enter email id'
end

我的Html代码是

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="form-fields">
    <%= f.label :email %>
    <%= f.email_field :email %>

    <%= f.label :password %>
    <%= f.password_field :password %>

    <%= f.submit "Log In" %>
  </div>
<% end %>

我的Jquery验证码是

  $('.new_user').validate({
    rules: {
      'user[email]': {
        required: true,
        email: true
      },
      'user[password]': {
        required: true,
        minlength: 8
      }
    },
    messages: {
      'user[email]': {
        required: 'Please enter email id',
        email: 'Please enter a VALID email id'
      },
      'user[password]': {
        required: 'Please enter a password',
        minlength: 'Password should be 8 characters long'
      }
    }
  });

0 个答案:

没有答案