[rspec-users] using restful_authentication current_user inside controller specs

court3nay court3nay at gmail.com
Tue Aug 21 03:10:48 EDT 2007


controller.stub!:(current_user).and_return(@user)

-------
Courtenay

On Aug 20, 2007, at 10:32 PM, "Andrew WC Brown" <omen.king at gmail.com>  
wrote:

> I'm using restful_authentication in my app and I have  the before  
> filters in my application rhtml:
>
> before_filter :login_required
> around_filter :set_timezone
> around_filter :catch_errors
>
> Currently I have them commented out while rspec'in but I'll need to  
> add them in my specs.
>
>
>   def create
>     @ticket = Ticket.new(params[:ticket])
>     @ticket.user = current_user
>     if @ticket.save
>       redirect_to tickets_path
>     else
>       render new_ticket_path(params[:user_id])
>     end
>   end
>
> describe TicketsController, "handling POST /tickets" do
>    before do
>      @ticket = mock_model(Ticket, :save => true)
>      @current_user = mock_model(User)
>      @params = {}
>    end
>    def do_post
>      post :create, :ticket => @params
>    end
>    it "should create a new ticket and assign current user as  
> ticket's user" do
>      @ticket.should_receive(:new).with(@params).and_return(@ticket)
>      assigns[:ticket].user.should equal(@current_user)
>      do_post
>    end
> end
>
> 1)
> NoMethodError in 'TicketsController handling POST /tickets should  
> create a new ticket and assign current user as ticket's user'
> You have a nil object when you didn't expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.include?
> /Volumes/EXTERNAL/web/yellowticket/vendor/plugins/rspec_on_rails/lib/ 
> spec/rails/dsl/assigns_hash_proxy.rb:10:in `[]'
> ./spec/controllers/tickets_controller_spec.rb:14:
>
>
> My guess is that I'm not allow to do this: assigns[:ticket].user
> How do I apple the assignment with current_user?
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list