cremes.devlist@mac.com
2006-May-06 14:20 UTC
[Rails] depot test-driven development exercise
(I posted this yesterday but within 20 minutes my thread was hijacked so I''m not sure anyone really saw this.) I''ve been working my way through the Agile book and just completed the depot sample application. I learned quite a bit along the way. On page 171 we are led through the creation of a test (that fails) for new code. The new code (a search function) is left as an exercise for the reader. Well, I think I solved it but I''m not sure I did it the "Rails way" (even though it passes the test). After much experimentation, here''s what I did: 1. Edit the depot/app/controller/search_controller.rb file: class SearchController < ApplicationController def search @query = String.new(params[:query]) @products = Product.find(:all, :conditions => "title like ''%#{@query} %'' OR description like ''%#{@query}%''") flash[:notice] = "Found #{@products.size} product(s)." end end 2. cp depot/app/views/layout/store.rhtml depot/app/views/layout/ search.rhtml 3. cp depot/app/views/store/index.rhtml depot/app/views/search/ search.rhtml 4. Edit depot/app/views/search/search.rhtml Add in another set of <div> tags around it so we have <div class="results"> as the outermost one. It now looks like: <div class="results"> <% @products.each do |product| %> <div class="catalogentry"> <img src="<%= product.image_url %>"/> <h3><%= h product.title %></h3> <%= product.description %> <span class="catalogprice"><%= fmt_dollars(product.price) %></ span> <%= link_to ''Add to Cart'', {:action => ''add_to_cart'', :id => product }, :class => ''addtocart'' %><br/> </div> <div class="separator"> </div> <% end %> </div> <%= link_to "Show my cart", :action => "display_cart" %> 5. Change the test so it does "assert_template ''search/search''" instead of "assert_template ''search/results''". 6. There is no step six. Was there a better or easier way of accomplishing this task? Also, why did the book want to test "assert_template ''search/ results''"? That seems like an error to me. The default view for a controller''s method (action) has the same name as the action, right? Unless you do a "redirect_to :action => ''results''" in which case the "assert_response :success" would now fail because we are redirecting to a page with a different name. This really confused me and caused me to flail around much longer than necessary to solve this problem. Or is the test correct and I can have an action named "search" rendered by a view named "results"? cr
-------------- Original message ---------------------- From: cremes.devlist@mac.com> Also, why did the book want to test "assert_template ''search/ > results''"? That seems like an error to me. The default view for a > controller''s method (action) has the same name as the action, right? > Unless you do a "redirect_to :action => ''results''" in which case the > "assert_response :success" would now fail because we are redirecting > to a page with a different name. This really confused me and caused > me to flail around much longer than necessary to solve this problem. > > Or is the test correct and I can have an action named "search" > rendered by a view named "results"?Bingo. It is essential to understand the difference between redirect_to :action => ''results'' and render :action => ''results''. The former doesn''t run any of your rhtml files. Instead it returns a "302 Moved" status to the client (rather than the typical 200 OK followed by an HTML page). The web browser sees this, and interprets it as a directive to make _another_ request against the server -- this time for the /search/results URL. It''s an entirely separate request, so it goes through the whole Routing -> Controller -> View thing all over again. That also means all the instance variables you set up in the action that *issued* the redirect aren''t available in the second request. The latter (render) just tells Rails to run a different rhtml page to determine what content to send back. It''s all the same request, and it''s just an internal Rails pipeline switcheroo, so the browser has no idea that any of it happened. Devin