如何在 Rails 3 中使用 Capybara 从选择框中选择日期? [英] How to select date from a select box using Capybara in Rails 3?

查看:34
本文介绍了如何在 Rails 3 中使用 Capybara 从选择框中选择日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RSpec 和 Capybara 为 Rails 3 项目中的控制器编写规范,我想从选择框中选择当前日期.我试过了:

I'm writing a spec for a controller in Rails 3 project using RSpec and Capybara, and I want to select current date from a select box. I tried:

select Date.today, :from => 'Date of birth'

但规范失败,我得到错误:

but the spec fails and I get error:

失败/错误:选择 Date.today,:from => '出生日期'无方法错误:2011 年 7 月 18 日星期一的未定义方法 `to_xpath':日期

Failure/Error: select Date.today, :from => 'Date of birth' NoMethodError: undefined method `to_xpath' for Mon, 18 Jul 2011:Date

如何解决?

附言在视图文件中,我使用 simple_form_for 标记,选择框由代码生成:

P.S. In view file I use simple_form_for tag and the select box is generated by code:

f.input :date_of_birth

推荐答案

您需要在 html 的选择菜单中指定确切的值.因此,如果您的选择具有2011/01/01"之类的值,那么您需要编写:

You need to specify the exact value as it's in the select menu in html. So if your select has values like "2011/01/01" then you need to write:

select '2011/01/01', :from => 'Date of birth'

您的代码失败,因为您传递了一个日期对象.

Your code fails because you pass a date object.

这篇关于如何在 Rails 3 中使用 Capybara 从选择框中选择日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆