Rspec压倒Capybara'在'黄瓜中的方法

时间:2015-04-08 17:23:50

标签: ruby-on-rails ruby rspec cucumber capybara

我正在使用黄瓜和水豚在大型消费者网站上测试帐户的创建。当我在常规撬开会话中运行我的水豚代码时,一切正常。但是当我运行黄瓜测试时,within块似乎没有运行,并且注册表单没有填写。

env.rb

require 'capybara/cucumber'
require 'selenium-webdriver'

Before do
  Capybara.default_driver = :selenium
end

test.feature

Scenario: Test consumer site account creation
* I create a new example.com account

step_definition.rb

When(/^I create a new account$/) do
  visit 'http://example.com/myaccount'
  find("#cboxClose").click if page.has_css?("#cboxClose")

  require 'pry';binding.pry
  within("form[name='newCustomer']") do
    random_letters = SecureRandom.urlsafe_base64(5)
    fill_in "Email Address", :with => "test-#{random_letters}@example.com"
    fill_in "Create Password", :with => "Password123"
    fill_in "Confirm Password", :with => "Password123"
    click_on "Create Account"
  end
end

当我运行cucumber并尝试在pry中运行within块时,我明白了:

#<RSpec::Matchers::AliasedMatcher:0x007ff5e86b16e8
 @base_matcher=
  #<RSpec::Matchers::BuiltIn::BeWithin:0x007ff5e86b1710
   @delta="form[name='newCustomer']">,
 @description_block=
  #<Proc:0x007ff5e4c34d08@/Users/anthonychung/.rvm/gems/ruby-2.1.2/gems/rspec-expectations-3.2.0/lib/rspec/matchers.rb:245 (lambda)>>

所以我怀疑RSpec的别名匹配器会以某种方式覆盖方法中的水豚。

当我尝试within(:css, "form[etc.]")时,我得到一个参数错误ArgumentError: wrong number of arguments (2 for 1)

1 个答案:

答案 0 :(得分:2)

page.within

我觉得自己很愚蠢,但至少我明白了。