开发者

Cucumber Help in Rails 3

I am very new to cucumber and rspec, but do have some rails and ruby understanding.

The gems in question are: Devise, Declarative_Authorization, and role_model

Rails 3 is what I am using and ruby 1.9.2-rc2

remember I do NOT know what I am doing

I have written a Feature:

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

With some web steps:

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 => 开发者_StackOverflow"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

My problem is that when I run the feature I receive an error saying undefined local variable or method `user_session' for #Cucumber::Rails::World:0x8077e1f8

I was under the assumption that user_session was a method that came with devise. How do I test if a user is a certain role?

Or better yet how do I tell cucumber about the methods that are in the rails app provided by gems? Thank you for your time, it is greatly appreciated. --iAm


Normally you shouldn't do this kinda tests in cucumber, it is too High level. You would normally be doing these in your specs, Not your features. In cucumber, you are going through a mock browser, so you don't have to setup all the helpers and the like. Just visit the web app like your a user, and login. the stack should do the rest.. I am using devise and here is the way I setup an admin.

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

As for testing a certain role, you would do that in rspec, not in cucumber. The way you would test in cucumber is go to page that only that role can see, and make sure you can see it, then make another test that goes to page that you shouldn't be able to see, and make sure you get the error you are expecting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜