views:

52

answers:

1

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 => "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

+1  A: 

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 = '[email protected]'
  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.

Doon
Thank you for your answer, I will change my approach and keep reading on the subject.
iAm
Another question I have is, should I start with a cucumber or rspec tests if I want to be true to BDD?
iAm
I reccommend Reading the Rspec book from Prag Press(http://pragprog.com/titles/achbd/the-rspec-book) but normally you write the Feature first, write the steps, build out the view logic using Red:green:refactor in Rspec, then the controller logic in rspec, then the objects, then back to cucumber and hopefully watch the step pass..
Doon
I do have the book, beta version, and am reading it, my real problem seems to be not being able to use methods like response.should have_text("text"), when I run the feature it fails because this method is undefined, but my thoughts are it(the method) is part of rspec-rails gem and it should know about it, and yes it is installed and it is in the gemfile, any thoughts on this?
iAm
Hmm not really, as I never try to use them directly in my features, preferring to use the capybara steps, ie Then I should see "foo", and let it handle it..
Doon
no the methods are not in the feature, rather in the steps that go with the feature
iAm
for the login step i use the method response.should have_text("Log in Successful"), i know that this method is available but for some reason it fails saying it don't know about it
iAm
let me ask you this, if I am still learning ruby and rails, should I even worry about cucumber+rspec, and learn them later?
iAm
The learning curve might be a bit rough learning ruby, rails, rspec, and cucumber, and getting them all working together can be a bit daunting. Hard to say. I learned ruby first, then rails. Then went to the different testing frameworks. as for the steps, are you using webrat or capybara, etc. What happens if you change it in the step, to read then ' page.should have_text("Log in Successful") ' does that work? Also checking for that should probably in a 'then' clause in the feature. IE one feature tests for login, and then I should see "Log in Successful".
Doon
if you are including this as part of the setup for another scenario, you don't 100% need to check for that part, as the step will fall on the next when/then anyway...
Doon
Doon -- thank you --I tried what you said with "page.should..." and got back undefined method `has_text?' for #<Capybara::Session:0x00000100ed69c0>, any idea what is wrong. is it with my setup, or my code
iAm
actually use this. page.should have_content("Log in Successful"), that should (as it is what the i should see ".*" step uses...
Doon
Yes, thank you :) do you know where I can find a list of all the methods like the have_content one, so I know what I can use
iAm
I would guess Read the web_steps.rb file, and look at the source of capybara. For all my cucumber testing I just use the steps included with web_steps and haven't run into a reason to go out side of them..
Doon