[rspec-users] cucumber is_admin? testing

David Chelimsky dchelimsky at gmail.com
Tue Nov 29 05:24:04 EST 2011

On Nov 28, 2011, at 10:07 PM, Alex Whiteland wrote:

>> 1. actually log in (i.e. create a user, go to the login screen and log
>> in). You can wrap this in a single step definition like "Given I am
>> logged in as 'admin'", but you still have to go through the app within
>> the step definition.
> I do this. Here is example:
> Scenario: guest becomes a user
>  Given I am guest
>  When I go to the signup_path
>  And puts signup info
>  Then new user should be created
>  And I should signin
>> Smth.
> Given /^I am guest$/ do
>  get_me_the_cookies.should eq([])
> end

^^ Givens, by definition, are given; to be taken for granted. They should not have expectations in them. If you feel the need to have an expectation in them, it suggests that there is another sceneario missing.

> When /^I go to the signup_path$/ do
>  visit signup_path
> end
> When /^puts signup info$/ do
>  fill_in "user_username", :with => "frankpopp"
>  fill_in "user_first", :with => "Frank"
>  fill_in "user_second", :with => "Popp"
>  fill_in "user_password", :with => "123456"
>  fill_in "user_password_confirmation", :with => "123456"
>  click_button "Sign up!"
> end
> Then /^new user should be created$/ do
>  page.should have_content("New user added: frankpopp")
> end

This doesn't tell you that the new user actually exists. Here I'd recommend going to the database:

Then /^new user should be created$/ do
  User.find_by_username("frankpopp").should_not be_nil

> Then /^I should signin$/ do
>  is_user?.should be_true
> end

Your email earlier this thread has an "is_admin?" method, so I'll assume "is_user?" works the same way:

def is_admin?
  @current_user && @current_user.id != 1

The problem is that @current_user is an instance variable inside the controller. You can't access it from a scenario. Usually I just use something something on the page to identify that "frankpopp" is signed in.

>> in users_controller.rb:
>  def create
>   @user = User.new(params[:user])
>   if @user.save
>    session[:user_id] = @user.id # This create a session
>    flash[:success] = "New user added: " + @user.username
>    flash[:notice] = "His password is: " + @user.password if is_admin?
>    redirect_to @user
>   else
>   end
>  end


More information about the rspec-users mailing list