[rspec-users] cucumber is_admin? testing

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


On Nov 29, 2011, at 4:24 AM, David Chelimsky wrote:

> 
> 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
> end
> 
>> 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?

^^ That should have been is_user?

>  @current_user && @current_user.id != 1
> end
> 
> 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.

Also, if you want to specify that frankpopp is an admin, you can either check the database via the model (i.e. User.find_by_username("frankpopp").should be_admin or some such), or, again, look for some identifying feature on the page - maybe a link that only admins can see.

>>> 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
> 
> HTH,
> David

BTW - while I do want to see you get the help you need, this conversation is noise for many rspec-users readers, so if you have all you need, please wait until you get your membership on the Cucumber list sorted out before continuing it. If things are still not working for you, however, please feel free to continue the conversation until this particular issue is resolved for you.

Cheers,
David


More information about the rspec-users mailing list