1
$ruby --version
ruby 1.9.3dev (2011-09-23 revision 33323) [i686-linux]

gem 'rails', '3.2.9'

group :test do
  gem 'cucumber-rails', require: false
  gem 'capybara', '~> 2.0.3'
  gem 'rspec-rails'
  gem 'database_cleaner'
  gem 'factory_girl'
  gem "ffaker"
  gem "capybara-firebug", "~> 1.3.0"
  gem 'ruby-debug19'
end

在我的黄瓜步骤之一中,我试图断言下拉元素(<select> 标记)具有使用 Capybara API 和以下代码选择的特定选项。

  within('#myForm') do
    page.has_select?("#event_type", selected: @event_type).should be_true
  end

但是我收到此错误:

   expected: true value
   got: false (RSpec::Expectations::ExpectationNotMetError)

我在我的场景中使用 (capybara-firebug) 的@firebug标签检查了 DOM ,发现在下拉列表中选择了所需的选项(由 @event_type 持有)但是在 DOM 中没有添加“selected”属性到我猜是导致期望失败的选择标签。

我在这里找到了类似的参考:https ://github.com/jnicklas/capybara/issues/171 ,其中提到了有修复的提交,但我猜该提交没有合并到我正在使用的水豚版本中。我正在尝试挖掘源代码并发现https://github.com/jnicklas/capybara/issues/171中提到的修复程序 不存在于 Capybara 的代码库中。

任何机构都可以让我知道如何断言选择标签选择了特定选项吗?

注意:在没有任何版本的 Gem 文件中使用 capybara 时:

gem 'capybara'

Capybara 版本 2.1.0 已启动安装,但失败并出现错误:

capybara requires Ruby version >= 1.9.3

虽然我有所需的红宝石版本。

以下是输出

  $ rvm info
  ruby-1.9.3-rc1:

system:
  uname:       "Linux jigneshgohel-Inspiron-N5110 3.2.0-40-generic-pae #64-Ubuntu SMP Mon Mar 25 21:44:41 UTC 2013 i686 i686 i386 GNU/Linux"
  bash:        "/bin/bash => GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)"
  zsh:         " => not installed"

rvm:
  version:      "rvm 1.13.4 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]"
  updated:      "11 months 4 days 14 hours 20 minutes 18 seconds ago"

ruby:
  interpreter:  "ruby"
  version:      "1.9.3dev"
  date:         "2011-09-23"
  platform:     "i686-linux"
  patchlevel:   "2011-09-23 revision 33323"
  full_version: "ruby 1.9.3dev (2011-09-23 revision 33323) [i686-linux]"

homes:
  gem:          "/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1"
  ruby:         "/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1"

binaries:
  ruby:         "/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1/bin/ruby"
  irb:          "/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1/bin/irb"
  gem:          "/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1/bin/gem"
  rake:         "/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1/bin/rake"

environment:
  PATH:         "/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1/bin:/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1@global/bin:/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1/bin:/home/jigneshgohel/.rvm/bin:/usr/local/heroku/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/media/work/Environment/Languages/Java/jdk1.7.0_04/bin:/media/work/Environment/BuildTools/apache-maven-3.0.4/bin:/media/work/Environment/BuildTools/apache-ant-1.8.3/bin"
  GEM_HOME:     "/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1"
  GEM_PATH:     "/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1:/home/jigneshgohel/.rvm/gems/ruby-1.9.3-rc1@global"
  MY_RUBY_HOME: "/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1"
  IRBRC:        "/home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1/.irbrc"
  RUBYOPT:      ""
  gemset:       "

以下是输出:

  $rvm list
  rvm rubies

  =* ruby-1.9.3-rc1 [ i686 ]

  # => - current
  # =* - current && default
  #  * - default
4

1 回答 1

1

I don't use cucumber, but in you example

within('#myForm') do
  page.has_select?("#event_type", selected: @event_type).should be_true
end

it depends on your dom, but you don't have to use '#' in the id selector here, and 'selected:' should be the text in the option, not the value.

so it should be (assuming @event_type is what you are displaying for the text)

within('#myForm') do
  page.has_select?("event_type", selected: @event_type).should be_true
end

or i'd prefer

page.should have_select('event_type', selected: @event_type)
于 2013-08-17T02:34:38.207 回答