【问题标题】:Cucumber Help in Rails 3Rails 3 中的黄瓜帮助
【发布时间】:2010-08-17 14:41:34
【问题描述】:

我对 cucumber 和 rspec 很陌生,但对 rails 和 ruby​​ 有一些了解。

有问题的宝石是:Devise、Declarative_Authorization 和 role_model

我正在使用 Rails 3 和 ruby​​ 1.9.2-rc2

记住我不知道我在做什么

我写了一个功能:

Feature: As the administrator of the site give me quick access to information
  In order to do lots of stuff
  As an administrator
  I want to see reports and stuff

  Background:
    Given a logged in user with a role of "admin"

  Scenario: Admin can see links
   Given I am on the admin dashboard page
   Then I should see all admin links

通过一些网络步骤:

module AdminDashboardHelpers

  def current_user_session
    return @user_session if defined?(@user_session)
    @user_session = user_session
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end

  def admin_signed_in?
    @user = current_user
    @user.has_role?(:admin)
  end
end

World(AdminDashboardHelpers)


Given /^a logged in user with a role of "([^\"]*)"$/ do |role|
  visit new_user_session_path
  fill_in "Login", :with => "iam"
  fill_in "Password", :with => "secret"
  click_button "Log in"
  admin_signed_in?
  #@current_user.should has_role?(role)
end

Then /^I should see all admin links$/ do
  pending # express the regexp above with the code you wish you had
end

我的问题是,当我运行该功能时,我收到一条错误消息 #Cucumber::Rails::World:0x8077e1f8 的未定义局部变量或方法 `user_session'

我假设 user_session 是设计附带的一种方法。 如何测试用户是否是某个角色?

或者更好的是,我如何告诉 cucumber gems 提供的 rails 应用程序中的方法? 感谢您的宝贵时间,非常感谢。 --iAm

【问题讨论】:

    标签: rspec ruby-on-rails-3 cucumber integration-testing


    【解决方案1】:

    通常你不应该在黄瓜上做这种测试,它太高了。您通常会在您的规格中执行这些操作,而不是您的功能。在 cucumber 中,您正在通过一个模拟浏览器,因此您不必设置所有帮助程序等。只需像您的用户一样访问网络应用程序,然后登录。堆栈应该完成其余的工作。我正在使用设计,这是我设置管理员的方式。

    Given /^I have one\s+administrator "([^\"]*)" with email "([^\"]*)" and password "([^\"]*)"$/ do |name,email, password|
      Administrator.new(:email => email,
                        :username=>name,
                        :password => password,
                        :password_confirmation => password).save!
    end
    
    Given /^I am an Authenticated Administrator$/ do
      name = 'admin'
      email = 'admin@inoc.net'
      password = 'secret!'
    
      Given %{I have one administrator "#{name}" with email "#{email}" and password "#{password}"}
      And %{I go to the administrator login page}
      And %{I fill in "administrator_username" with "#{name}"}
      And %{I fill in "administrator_password" with "#{password}"}
      And %{I press "Sign in"}
    end
    
    Given /^I have not authenticated as an+ "([^"]*)"$/ do |role|
         visit("/#{role}s/sign_out")
    end
    

    至于测试某个角色,你会在 rspec 中进行,而不是在 cucumber 中。您在 Cucumber 中测试的方式是转到只有该角色可以看到的页面,并确保您可以看到它,然后进行另一个测试,进入您不应该看到的页面,并确保您获得您期望的错误。

    【讨论】:

    • 感谢您的回答,我将改变我的方法并继续阅读该主题。
    • 我的另一个问题是,如果我想忠于 BDD,我应该从黄瓜测试还是 rspec 测试开始?
    • 我建议从 Prag Press(pragprog.com/titles/achbd/the-rspec-book) 阅读 Rspec 书籍,但通常您首先编写功能,编写步骤,使用 Rspec 中的 Red:green:refactor 构建视图逻辑,然后rspec 中的控制器逻辑,然后是对象,然后返回 cucumber 并希望看到步骤通过..
    • 我确实有这本书,测试版,并且正在阅读它,我真正的问题似乎是当我运行该功能时无法使用 response.should have_text("text") 之类的方法它失败了,因为这个方法是未定义的,但我的想法是它(方法)是 rspec-rails gem 的一部分,它应该知道它,是的,它已安装并且它在 gemfile 中,对此有什么想法吗?跨度>
    • 嗯不是真的,因为我从不尝试直接在我的功能中使用它们,更喜欢使用水豚步骤,即然后我应该看到“foo”,让它处理它..
    猜你喜欢
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多