我们先初步画了些草图来整理我们思路和设计然后开始写代码对于具体实现我们已经有了大致思路但是如果有更多反馈信息话会有助于我们走在正确道路上我们会在深入到代码的前编写测试代码考虑我们代码将怎样工作确定些规约当测试通过你代码就OK了 现在我们来考虑下查询功能测试应该由哪个controller来控制查询操作呢?用户和管理员都可以进行查询操作我们可以在store_controller.rb或者admin_controller.rb中添加个searchAction但是这里我们添加个SearchController并且含有个思路方法search在rails命令行执行命令:
depot>ruby script/generate controller Search search
我们看到在app/controllers和test/functional目录下已经生成了对应文件但是现在我们并不关心SearchControllersearch思路方法实现我们关心是在测试时我们所期望看到结果现在添加测试代码在test/functional/search_controller_test.rb中添加test_search思路方法:
我们首先想到是searchAction然后判断是否得到了响应:
get :search, :title => "Pragmatic Version Control"
assert_response :success
根据的前草图我们应该在页面上显示个flash信息所以我们要判断flash信息文本以及是否显示了正确页面:
assert_equal "Found 1 product(s).", flash[:notice]
assert_template "search/results"
然后我们想要判断查询所得到商品信息:
products = assigns(:products)
assert_not_nil products
assert_equal 1, products.size
assert_equal "Pragmatic Version Control", products[0].title
我们还想判断用来显示查询结果页面些内容我们查询到商品会作为列表在页面上显示我们使用catelog视图相同css样式:
assert_tag :tag => "div",
:attributes => { : => "results" },
:children => { :count => 1,
:only => { :tag => "div",
:attributes => { : => "catalogentry" }}}
下面是完整测试思路方法:
def test_search
get :search, :title => "Pragmatic Version Control"
assert_response :success
assert_equal "Found 1 product(s).", flash[:notice]
assert_template "search/results"
products = assigns(:products)
assert_not_nil products
assert_equal 1, products.size
assert_equal "Pragmatic Version Control", products[0].title
assert_tag :tag => "div",
:attributes => { : => "results" },
:children => { :count => 1,
:only => { :tag => "div",
:attributes => { : => "catalogentry" }}}
end
现在我们来运行测试:ruby test/functional/search_controller_test.rb
不出意外会得到下面结果:
test_search(SearchControllerTest) [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那我们就自己来步步实现searchAction
1. 给search思路方法添加内容:
@products = Product.find(:all,:conditions=>['title=?',params[:title]])
not @products.nil?
flash[:notice] = sprf('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
----------------------------------------------------------------------------
2. 这次轮到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=>{:=>"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 ="results">
<div = "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.
1 tests, 7 assertions, 0 failures, 0 errors
----------------------------------------------------------
4. 在实现search.rhtml和results.rhtml时候我碰到了些问题用测试用例都可以选出数据来但是通过页面就如何也不行了把log里sql贴出来到phpMyAdmin里执行也能选出数据真不知道是如何回事自己对rails理解还不深自己胡乱写了这些代码先把代码都帖出来等自己对rails有更深入理解时候看能不能找到问题同时也请高人指点
search_controller_test.rb:
require File.dirname(__FILE__) + '/../test_helper'
require 'search_controller'
# Re-raise errors caught by the controller.
SearchController; def rescue_action(e) raise e end; end
SearchControllerTest < Test::Unit::TestCase
fixtures :products
def up
@controller = SearchController.
@request = ActionController::TestRequest.
@response = ActionController::TestResponse.
end
def test_search
get :search, :title => "Pragmatic Version Control"
assert_response :success
assert_equal "Found 1 product(s).", flash[:notice]
assert_template "search/results"
products = assigns(:products)
assert_not_nil products
assert_equal 1, products.size
assert_equal "Pragmatic Version Control", products[0].title
assert_tag :tag => "div",
:attributes => { : => "results" },
:children => { :count => 1,
:only => { :tag => "div",
:attributes => { : => "catalogentry" }}}
end
end
search_controller.rb
SearchController < ApplicationController
def search
pr(params[:title])
@products = Product.find(:all,:conditions=>['title=?',params[:title]])
not @products.nil?
flash[:notice] = sprf('Found %d product(s).',@products.size)
end
pr(flash[:notice])
#redirect_to(:action=>'results')
render(:action=>'results')
end
def results
end
def index
end
end
Views下有 3个文件:index.rhtmlresults.rhtmlsearch.rhtml
index.rhtml:
<html>
<%= form_tag(:action=>'search',:id=>@products) %>
<table>
<tr>
<td>Book title:</td>
<td><%=text_field("title","")%></td>
</tr>
<tr>
<td><input type="submit" value="SEARCH" /></td>
</tr>
</table>
<%= end_form_tag %>
</html>
results.rhtml:
<h1>Search#results</h1>
<p>Find me in app/views/search/results.rhtml</p>
<div ="results">
<div = "catalogentry">
<table cellpadding="10" cellspacing="0">
<tr ="carttitle">
<td >title</td>
<td >description</td>
<td >price</td>
</tr>
<%
prf("result:%d",@products.size)
for product in @products
-%>
<tr>
<td><%= product.title %></td>
<td><%= product.description%></td>
<td align="right"><%= fmt_dollars(product.price) %></td>
</tr>
<% end %>
</table>
</div>
</div>
search.rhtml:
<html></html>
最新评论