Ken,

I've sometimes found extracting this code into a helper like

def login(user)
  @controller.stub(:authenticate_user!)
  @controller.stub(:current_user).and_return(user)
  @controller.stub(:add_secure_model_data)
  user
end

and

before(:each)
  @user = login(User.new)
end

because most of my controller spec files have to have specs covering
unautheticated attempts to access a secured action, or the User may
have to be constructed in different ways, and this approach makes it
easier to have scenarios for both authenticated and unauthenticated
access in the same spec. YMMV.

Best,
Sidu.
http://c42.in
http://about.me/ponnappa

On 26 May 2011 02:20, Ken Egervari <ken.egerv...@gmail.com> wrote:
> I'd like to factor this bunch of code so that all of my controller tests
> (well, almost all of them) use this before(:each) block:
>
>   before(:each) do
>     @user = User.new
>     controller.stub(:authenticate_user!)
>     controller.stub(:current_user).and_return(@user)
>     controller.stub(:add_secure_model_data)
>   end
>
> Is there any way to do that? I don't want to include it in all
> controllers... because there are a few that don't need this. By basically,
> every controller that extends from SecureController will need this
> before(:each) block.
>
> Is there any nice way to do that?
>
> Thanks
>
> Ken
>
> _______________________________________________
> 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