Hi folks, I want to test the following: In sites controller: def create @site = Site.new(params[:site]) ... In the functional test: class SitesControllerTest < Test::Unit::TestCase def setup @controller = SitesController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end ... def test_create ... How can I mock the parameters passed to the new method of the Site model to test the create method of the sites controller? Thank you! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Feb 2008, at 11:34, Alvaro Perez wrote:> > Hi folks, > > ... > > def test_create > ... > > How can I mock the parameters passed to the new method of the Site > model > to test the create method of the sites controller? >If i''ve understood what you want, something like Site.expects(:new).with( :name => ''foo'', :location => ''bar'') will do the job. Fred> Thank you! > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 22 Feb 2008, at 11:34, Alvaro Perez wrote: > >> to test the create method of the sites controller? >> > If i''ve understood what you want, something like > Site.expects(:new).with( :name => ''foo'', :location => ''bar'') > will do the job. > > FredThat I''ve already tried, but does not work: NoMethodError: 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.[] -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Feb 2008, at 11:49, Alvaro Perez wrote:> > Frederick Cheung wrote: >> On 22 Feb 2008, at 11:34, Alvaro Perez wrote: >> >>> to test the create method of the sites controller? >>> >> If i''ve understood what you want, something like >> Site.expects(:new).with( :name => ''foo'', :location => ''bar'') >> will do the job. >> >> Fred > > That I''ve already tried, but does not work: > > NoMethodError: 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.[] >Where does that error occur ? You are aware that setting an expectation completely replaces the original method ? (ie Site.new will now return nil, unless you do Site.expects(:new).returns(x) in which case it returns x) Fred> -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 22 Feb 2008, at 11:49, Alvaro Perez wrote: > >>> Fred >> >> That I''ve already tried, but does not work: >> >> NoMethodError: 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.[] >> > Where does that error occur ? You are aware that setting an > expectation completely replaces the original method ? (ie Site.new > will now return nil, unless you do Site.expects(:new).returns(x) in > which case it returns x) > > FredThat''s what I though, but the behaviour is different. The error happens in the line when the method new is called: def create @site = Site.new(params[:site]) <--- here -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/22/08, Alvaro Perez <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi folks, > > I want to test the following: > > In sites controller: > > def create > @site = Site.new(params[:site]) > ... > > In the functional test: > > class SitesControllerTest < Test::Unit::TestCase > def setup > @controller = SitesController.new > @request = ActionController::TestRequest.new > @response = ActionController::TestResponse.new > end > > ... > > def test_createpost :create, :site => ... -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---