Hi, I am writing a functional test for a controller. The problem I have is that a before filter defined in ApplicationController is hit and I don''t know how to stub it. I tried controller.expects(:find_site).returns(@site) but it fails as controller is unknown method. I tried ApplicationController.expects(:find_site).returns(@site) but the assertion fails as it never happens. So how do I deal with that? -- 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 -~----------~----~----~----~------~----~------~--~---
I tried the following: def setup @controller = ProductsController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @site = Site.new(:domain_name => ''example.com'') @controller.expects(:find_site).returns(@site) end But with the debugger, once inside the action of the controller being tested, @site is not set, and therefore the action cannot run correctly. -- 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 -~----------~----~----~----~------~----~------~--~---
Brandon Olivares
2009-Apr-16 15:35 UTC
Re: Stubbing an ApplicationController before_filter
Hi, The RSpec Book (http://www.pragprog.com/titles/achbd/the-rspec-book) just added a new chapter about testing controllers, within which is an example of testing a before filter. You might find it of use. Brandon> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Fernando Perez > Sent: Thursday, April 16, 2009 10:40 AM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Stubbing an ApplicationController before_filter > > > Hi, > > I am writing a functional test for a controller. The problem I have is > that a before filter defined in ApplicationController is hit and I > don''t > know how to stub it. > > I tried controller.expects(:find_site).returns(@site) but it fails as > controller is unknown method. > > I tried ApplicationController.expects(:find_site).returns(@site) but > the > assertion fails as it never happens. So how do I deal with that? > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brandon Olivares wrote:> Hi, > > The RSpec Book (http://www.pragprog.com/titles/achbd/the-rspec-book) > just added a new chapter about testing controllers, within which is an > example of testing a before filter. You might find it of use. > > BrandonI stopped using RSpec, and switched back to Test::Unit, the Agile Web dev with Rails has a good intro chapter and I was able to solve my problem. -- 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 -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2009-Apr-17 13:13 UTC
Re: Stubbing an ApplicationController before_filter
Fernando Perez wrote: [...]> I stopped using RSpec,Just for this issue? If so, there was no need. I use controller.stub!(:filter).return(@whatever) regularly with RSpec. It works fine.> and switched back to Test::Unit, the Agile Web > dev with Rails has a good intro chapter and I was able to solve my > problem.How were you able to solve the problem? It will help others if you post your solution. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 -~----------~----~----~----~------~----~------~--~---
> Just for this issue? If so, there was no need.Not just for that issue, and Brandon probably assumed I was using rspec, but it pissed me off some time ago. Each time I would hit a wall with RSpec and have to search for hours how to solve it. With Test::Unit for some reason, I don''t have as many little annoying troubles, and when I do, I find a solution whether on my own or by reading stuff online very quickly. I am having a much better testing experience with Test::Unit.> > How were you able to solve the problem? It will help others if you post > your solution.Well I can''t directly stub a controller instance method. So I stubbed the Model class method that the instance variable will be set to. -- 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 -~----------~----~----~----~------~----~------~--~---
Brandon Olivares
2009-Apr-17 14:48 UTC
Re: Stubbing an ApplicationController before_filter
> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Fernando Perez > Sent: Friday, April 17, 2009 9:30 AM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Stubbing an ApplicationController before_filter > > > > > Just for this issue? If so, there was no need. > Not just for that issue, and Brandon probably assumed I was using > rspec,Only because I could have sworn I saw you posting over on that mailing list just the other day. Brandon> but it pissed me off some time ago. > > Each time I would hit a wall with RSpec and have to search for hours > how > to solve it. With Test::Unit for some reason, I don''t have as many > little annoying troubles, and when I do, I find a solution whether on > my > own or by reading stuff online very quickly. I am having a much better > testing experience with Test::Unit. > > > > > How were you able to solve the problem? It will help others if you > post > > your solution. > > Well I can''t directly stub a controller instance method. So I stubbed > the Model class method that the instance variable will be set to. > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Only because I could have sworn I saw you posting over on that mailing > list just the other day.I didn''t fully transition to test::unit yet, so I still use RSpec''s ML from time to time. -- 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 -~----------~----~----~----~------~----~------~--~---