[rspec-users] Stubbing Login Stuff

s.ross cwdinfo at gmail.com
Sun Apr 15 15:11:45 EDT 2007


That was the magic. controller. As it turns out, this doesn't have to  
happen in setup, so I can specify both the positive and negative  
specification in one describe, as:

describe "an AdminController login process" do
   controller_name :admin
   integrate_views

   it "should deny access if you aren't logged in" do
     # stuff to determine that user gets a login page
   end

   it "should permit access if you are logged in" do
     controller.stub!(:validate_login).and_return(true)
     # stuff to determine success
   end
end

Is there a benefit to using two describe blocks, one with a setup/ 
stub! and the other without?

Steve

On Apr 15, 2007, at 11:22 AM, Pat Maddox wrote:

> On 4/15/07, s.ross <cwdinfo at gmail.com> wrote:
>> For some reason, I'm completely blanking on how to do this... Sorry
>> for the lots-o-code example.
>>
>> What I'm trying to do: stub the validate_login method in
>> application_controller so it returns true (user is logged in).
>> What happens: validate_login method in application_controller.rb is
>> called instead of my stub.
>
> In your setup (or wherever), do
>
> controller.stub!(:validate_login).and_return true
>
> When you called stub! without an explicit receiver, the message gets
> sent to self, which is a context.  Then when you called stub! on
> ApplicationController, it created a class-level method.  In Rails,
> each request creates a new controller instance, so you need to call
> stub! on that instance.
>
> Pat
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list