[rspec-users] cucumber is_admin? testing
dchelimsky at gmail.com
Mon Nov 28 11:10:01 EST 2011
On Mon, Nov 28, 2011 at 9:38 AM, Alex Whiteland <lists at ruby-forum.com> wrote:
> maybe I post an initiate letter here, and then find a way to write you?
>> Started here
> I have trite signin system, like this:
> include SessionsHelper
> helper_method :current_user
> def current_user
> @current_user ||= User.find(session[:user_id]) if session[:user_id]
> def is_admin?
> @current_user && @current_user.id == 1
> So, if I test
>> is_admin?.should be_true
> it returns:
> expected nil to be true (RSpec::Expectations::ExpectationNotMetError)
> ./features/step_definitions/users/add_user.rb:23:in `/^I should
> features/users/add_user.feature:13:in `And I should signin'
> But I signed in! Why? What's the way to test authentication system from
>> that is added to cucumber's world does not have access to your
>> controller's @current_user variable.
> So, I understand this. But how I can avoid this defect?
It is not a defect. It's how cukes work. They wrap Rails integration
tests, which don't give you direct access to controllers or sessions.
Your options are:
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.
2. This only works for in-memory scenarios (i.e. it doesn't work
in-browser when the app is running in a separate process) - you _can_
stub the controller using any_instance:
> If I paste def is_admin? into application_controller near current_user
> method, I wouldn't can World(ApplicationController). If I copy
> current_user code to sessions_helper, then session[:user_id] wouldn't
> Regards, Alex Whiteland
> Posted via http://www.ruby-forum.com/.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users