I''m trying to run some functional tests on Rails. However I am coming up with the following error. RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id app/controllers/posts_controller.rb:11:in `create'' posts_controller_test.rb:5:in `test_should_create'' Here is my test file require File.dirname(__FILE__) + "/../test_helper" class PostsControllerTest < ActionController::TestCase def test_should_create post :create, :post => { :message => ''Sausage and Egg Sandwich''} end end Here is my controller class PostsController < ApplicationController def index @posts = Post.all(:order => "created_at DESC") respond_to do |format| format.html end end def create @post = Post.create(:message => params[:post]) @post.user_id = current_user.id respond_to do |format| if @post.save format.html { redirect_to posts_path } format.js else flash[:notice] = "Message failed to save." format.html { redirect_to posts_path } end end end end ____ I have the same error when trying to writer other functional tests, in other models which involve creating a record in my project. Antony -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 April 2012 15:09, sherpa derpa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m trying to run some functional tests on Rails. However I am coming up > with the following error. > > > RuntimeError: Called id for nil, which would mistakenly be 4 -- if you > really wanted the id of nil, use object_id > app/controllers/posts_controller.rb:11:in `create'' > posts_controller_test.rb:5:in `test_should_create'' > > Here is my test file > > require File.dirname(__FILE__) + "/../test_helper" > > class PostsControllerTest < ActionController::TestCase > def test_should_create > post :create, :post => { :message => ''Sausage and Egg Sandwich''} > end > end > > Here is my controller > > class PostsController < ApplicationController > def index > @posts = Post.all(:order => "created_at DESC") > respond_to do |format| > format.html > end > end > > def create > @post = Post.create(:message => params[:post]) > -dbDBDwo96HhQFI55V6+gNQ@public.gmane.org_id = current_user.idAssuming that it is this line that is causing the error, what is the value of current_user when you run the test? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #1055633:> On 9 April 2012 15:09, sherpa derpa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> class PostsController < ApplicationController >> def index >> @posts = Post.all(:order => "created_at DESC") >> respond_to do |format| >> format.html >> end >> end >> >> def create >> @post = Post.create(:message => params[:post]) >> @post.user_id = current_user.id > > Assuming that it is this line that is causing the error, what is the > value of current_user when you run the test? > > ColinNil. Because I haven''t specified it, and the user isn''t logged in. I''ve tried invoking the current_user or writing user_id within the test, but that hasn''t worked. You need to be logged in order to write a post, but I wasn''t sure that mattered when writing the test? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 April 2012 15:30, sherpa derpa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1055633: >> On 9 April 2012 15:09, sherpa derpa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> >>> class PostsController < ApplicationController >>> def index >>> @posts = Post.all(:order => "created_at DESC") >>> respond_to do |format| >>> format.html >>> end >>> end >>> >>> def create >>> @post = Post.create(:message => params[:post]) >>> -dbDBDwo96HhQFI55V6+gNQ@public.gmane.org_id = current_user.id >> >> Assuming that it is this line that is causing the error, what is the >> value of current_user when you run the test? >> >> Colin > > Nil. Because I haven''t specified it, and the user isn''t logged in. I''ve > tried invoking the current_user or writing user_id within the test, but > that hasn''t worked. > > You need to be logged in order to write a post, but I wasn''t sure that > mattered when writing the test?Of course it matters, the tests should, for example, check that you cannot do things that you should not be able to do without logging in. How to accomplish this depends on how your authentication is done. Perhaps you need to set something in the session before running the test. Google should show you how to do that. Colin> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you Colin -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
sherpa derpa wrote in post #1055638:> Thank you ColinI''m using nifty-authentication for what it''s worth -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Really struggling attempting to implement this globally. Any suggestions to those who have used nifty_authentication? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 April 2012 18:17, sherpa derpa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Really struggling attempting to implement this globally. > > Any suggestions to those who have used nifty_authentication?Once again you have not quoted previous messages so anyone finding this would have difficulty following the thread. I don''t know about nifty_authentication but for authlogic the logon action actually creates a UserSession, so to logon in the test one does something like UserSession.create( users(:test_user) ) Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #1055698:> On 9 April 2012 18:17, sherpa derpa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Really struggling attempting to implement this globally. >> >> Any suggestions to those who have used nifty_authentication? > > Once again you have not quoted previous messages so anyone finding > this would have difficulty following the thread. I don''t know about > nifty_authentication but for authlogic the logon action actually > creates a UserSession, so to logon in the test one does something like > UserSession.create( users(:test_user) ) > > ColinThank you Colin -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.