On Mon, Nov 28, 2011 at 9:38 AM, Alex Whiteland <li...@ruby-forum.com> wrote:
> wtf?
>
> maybe I post an initiate letter here, and then find a way to write you?
>
>> Started here
> Hi!
>
> I have trite signin system, like this:
>
>> ApplicationController:
> include SessionsHelper
>
> private
>
> helper_method :current_user
>
> def current_user
>  @current_user ||= User.find(session[:user_id]) if session[:user_id]
> end
>
>
>> SessionsHelper:
>  def is_admin?
>   @current_user && @current_user.id == 1
>  end
>
>
>> features/support/env.rb:
> World(SessionsHelper)
>
> 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
> signin$/'
>      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
> scratch?
>
>> that is added to cucumber's world does not have access to your
>> application
>> 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:

  FooController.any_instance.stub(:current_user).and_return(user)

HTH,
David

>
> 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
> work.
>
> Regards, Alex Whiteland
>> end
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to