Andrew WC Brown
2007-Aug-21 05:32 UTC
[rspec-users] using restful_authentication current_user inside controller specs
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
aslak hellesoy
2007-Aug-21 07:01 UTC
[rspec-users] using restful_authentication current_user inside controller specs
@ticket.should_receive(:new).with(@params).and_return(@ticket) is like saying: @ticket = @ticket.new(params) And that, of course, doesn''t make much sense. I''ll give you a chance to find the solution yourself ;-) Aslak On 8/21/07, 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 >
court3nay
2007-Aug-21 07:10 UTC
[rspec-users] using restful_authentication current_user inside controller specs
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