如果我使用CanCan load_and_authorize_resource,Capybara找不到元素

时间:2013-12-30 21:19:18

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

我是Cucumber的新手,我正在编写我的第一个测试。当我尝试在UsersController中包含CanCan load_and_authorize_resource函数时,我遇到了问题。如果我发表评论,请删除该行所有内容。

我的能力:

if user.nil?
    cannot :manage, :all
elsif user.have_the_role? :user
    can :read, User
    # this allowes me to update just my profile
    can :update, User, :id => user.id
elsif user.have_the_role? :admin
    can :manage, :all
end

我的特色:

Scenario: New User
  Given I login with my google_apps account
  When I am redirected to create my profile
  And I fill in "First Name" with "Ricardo"
  And I fill in "Last Name" with "Berdejo"
  And I fill in "National Identifier" with "123456"
  And I select "M" from "Gender"
  And I select "April 26, 1990" as the "Date of Birth" date
  And I fill in "Cellphone Number" with "000000000"
  And I fill in "Personal Email" with "personal@email.com"
  And I fill in "Home Address" with "some direction"
  And I fill in "Title" with "Developer"
  And I fill in "Healthcare Company" with "EPS"
  And I fill in "Emergency Contact Name" with "Mom"
  And I fill in "Emergency Contact Phone" with "9999999"
  And I select "O" from "Blood Group"
  And I select "Positive" from "RH Factor"
  And I press "Save"
  Then I should see "Profile Saved"

我的步骤:

When(/^I fill in "(.*?)" with "(.*?)"$/) do |field, value|
  fill_in field, :with => value
end

错误:

Unable to find field "First Name" (Capybara::ElementNotFound)

提前谢谢

2 个答案:

答案 0 :(得分:0)

我发现测试代码是使用Google Apps帐户登录的。如果您的测试实际上是重定向到Google进行登录,那么您需要确保使用可以处理重定向到外部网站的Capybara驱动程序。默认驱动程序不能。

有关详细信息,请参阅https://github.com/jnicklas/capybara#selecting-the-driver

答案 1 :(得分:0)

我能解决它。对于未来的读者:登录前检查角色分配。 创建一个类似

的步骤
Given I am a User

Given I am a Admin

并在您的步骤中创建具有给定登录名/密码和角色的用户。

Given(/^I am an Admin$/) do
  @admin = Fabricate(:admin) do
    email "enter_your_mail_here"
    user_field_1 "Field"
    user_field_2 "Field2"
    role { Fabricate(:role, account_type: "admin") }
  end
end

希望这有帮助