Ruby on rails开发从头来(windows)(二十七)- 测试驱动开发 - 编程入门网
earchControllerTest) [test/functional/search_controller_test.rb:17]:
<"Found 1 product(s)."> expected but was
<nil>.1 tests, 2 assertions, 1 failures, 0 errors
因为我们还没有设置flash的内容,进一步说,我们还没有实现search这个Action。怎样实现,书上给留了个作业。OK,那我们就自己来一步步实现search的Action。 1.给search方法添加内容: @products = Product.find(:all,:conditions=>[''title=?'',params[:title]]) if not @products.nil? flash[:notice] = sprintf(''Found %d product(s).'',@products.size) end render(:action=>''results'')现在运行测试,结果如下: ---------------------------------------------------------------------------- 1) Failure: test_search(SearchControllerTest) [Z:/study/ruby/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack…… Z:/study/ruby/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack…… test/functional/search_controller_test.rb:19:in `test_search'']: expecting <"search/results"> but rendering with <"search/search"> 1 tests, 3 assertions, 1 failures, 0 errors ---------------------------------------------------------------------------- Ruby on rails开发从头来(windows)(二十七)- 测试驱动开发(3)时间:2011-12-02 博客园 Cure2.这次轮到assert_template "search/results"断言出错了,是因为我们还没有results这个View,我们在view目录下添加一个results.rhmtl文件,在search_controller.rb文件中添加一个results的空方法: def results end 还要在search方法中添加一句:render("search/results"),然后再运行测试,结果如下: ---------------------------------------------------------------------------- Finished in 0.125 seconds. 1) Failure: test_search(SearchControllerTest) [Z:/study/ruby/InstantRails/ruby/lib/ruby/gems/1.8/gems/…… Z:/study/ruby/InstantRails/ruby/lib/ruby/gems/1.8/gems/…… expected tag, but no tag found matching {:attributes=>{:class=>"results"}, :tag=>"div", "<h1>Search#results</h1>\n<p>Find me in app/views/search/results.rhtml</p>\n".<nil> is not true. ---------------------------------------------------------------------------- 3.现在可以看到,就只有最后一个断言assert_tag没有通过了,这个断言是对页面上的元素进行判断的,所以我们来实现results页面。仔细看看断言的内容,我们就知道只要在results.rhtml里添加两个div就可以了,下面是results.rhtml的完整内容: <h1>Search#results</h1> <p>Find me in app/views/search/results.rhtml</p> <div class="results"> <div class = "catalogentry"> </div> </div> 保存,然后再运行测试,激动人心的时刻来临了,所有的断言都通过了!测试OK了,下面是结果: ---------------------------------------------------------- DEPRECATION WARNING: You called render(''search/result…… t Z:/study/ruby/InstantRails/ruby/lib/ruby/gems/1.8/g…… Finished in 0.094 seconds. |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |