Bryan Helmkamp
2006-Oct-17 20:32 UTC
[mocha-developer] Mocking the rendering of a Rails template
Hello all, I''m having difficulty setting up mocks such that I can verify that Rails was going to render the right template file (for example new.rhtml) and stop Rails from performing the actual rendering. After peeking at Rails'' internals, I tried two techniques as illustrated here: http://pastie.caboo.se/18197 Neither worked. I think the first one failed because Rails doesn''t setup response.template to be an instance of ActionView::Base until after the controller processing is called. I have no idea what is going on with the second attempt. It''s hitting a mock, but I have no idea where that mock is coming from. Logically, it probably is coming from the any_instance magic, but in that case it should be expecting the render_file call. Thoughts? What am I doing wrong in the second case? Is there another way I could mock this? Any help would be greatly appreciated. -Bryan -- http://www.MyCongress.org/ -- coming soon
Chris Roos
2006-Oct-17 21:49 UTC
[mocha-developer] Mocking the rendering of a Rails template
On 10/17/06, Bryan Helmkamp <bhelmkamp at gmail.com> wrote:> Hello all, > > I''m having difficulty setting up mocks such that I can verify that > Rails was going to render the right template file (for example > new.rhtml) and stop Rails from performing the actual rendering. > > After peeking at Rails'' internals, I tried two techniques as illustrated here: > > http://pastie.caboo.se/18197 > > Neither worked. > > I think the first one failed because Rails doesn''t setup > response.template to be an instance of ActionView::Base until after > the controller processing is called. > > I have no idea what is going on with the second attempt. It''s hitting > a mock, but I have no idea where that mock is coming from. Logically, > it probably is coming from the any_instance magic, but in that case it > should be expecting the render_file call. Thoughts? > > What am I doing wrong in the second case? Is there another way I > could mock this? >I''m wondering how you''ve arrived in this situation. It seems to me that you may be trying to test rails code, rather than your own code (but that may be due to the simplified example given). Can you explain exactly what you''d like your tests to illustrate? Chris
James Mead
2006-Oct-17 22:01 UTC
[mocha-developer] Mocking the rendering of a Rails template
On 17/10/06, Bryan Helmkamp <bhelmkamp at gmail.com> wrote:> > I''m having difficulty setting up mocks such that I can verify that > Rails was going to render the right template file (for example > new.rhtml) and stop Rails from performing the actual rendering. > > After peeking at Rails'' internals, I tried two techniques as illustrated > here: > > http://pastie.caboo.se/18197 > > Neither worked. > > I think the first one failed because Rails doesn''t setup > response.template to be an instance of ActionView::Base until after > the controller processing is called. > > I have no idea what is going on with the second attempt. It''s hitting > a mock, but I have no idea where that mock is coming from. Logically, > it probably is coming from the any_instance magic, but in that case it > should be expecting the render_file call. Thoughts? > > What am I doing wrong in the second case? Is there another way I > could mock this? >Why do you need to get into the Rails internals? I think it would be better to mock a part of the published API e.g. the #render method on @controller. Otherwise you are effectively testing Rails itself. -- James. http://blog.floehopper.org -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20061017/7a29c103/attachment-0001.html
Kevin Clark
2006-Oct-17 22:11 UTC
[mocha-developer] Mocking the rendering of a Rails template
Bryan, I wouldn''t worry about cutting off the render path. Render doesn''t take that long generally (in comparison to db hits) and there''s a few cool features that make it nice to test views (like being able to access what template and layout were rendered). -- Kevin Clark http://glu.ttono.us
Bryan Helmkamp
2006-Oct-18 16:26 UTC
[mocha-developer] Mocking the rendering of a Rails template
I think you guys are right, I''m probably stumbling down the wrong path. Let me try to answer your questions about how I ended up here.... Kevin: I want to cut off the render path because I don''t want to test my views in my controller unit tests. Also, if I don''t cut off the render, I need my mock models to respond decently to all sorts of queries the views would be sending them. I don''t want to have to write those. Chris and James: I want my test to illustrate that my action never calls the #render method on controller. This means I can be sure the default template is rendering. So, basically, the expectation should be: @controller.expects(:render).never The problem is, Rails'' crafty internals call the @controller#render method even when I don''t, so that expecation will never work. Thoughts? -Bryan On 10/17/06, Kevin Clark <kevin.clark at gmail.com> wrote:> Bryan, > I wouldn''t worry about cutting off the render path. Render doesn''t > take that long generally (in comparison to db hits) and there''s a few > cool features that make it nice to test views (like being able to > access what template and layout were rendered). > > > -- > Kevin Clark > http://glu.ttono.us > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- http://www.MyCongress.org/ -- coming soon