[rspec-users] using restful_authentication current_user inside controller specs

Andrew WC Brown omen.king at gmail.com
Tue Aug 21 01:32:52 EDT 2007


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070821/e6e65469/attachment.html 


More information about the rspec-users mailing list