複数パターンのテスト

6.複数パターンのテスト
機能テストではフィクスチャで初期データを定義できます。しかし、これでは「横の計算」に関わるパターンのテストはできますが、「縦の計算」のパターンのテストは一つしかできません。たとえばデータの前後関係のテストでいえば0件の場合、1件の場合(前後とも関係がない)、2件の場合(前か後のみ関係がある)、3件の場合(前も後も関係がある)と4つの状態を考える必要がありますが、このうちのどれか一つしか準備できません。

6-1.プロジェクトの生成
(1) プロジェクトTest002の生成
(2) 日本語環境の設定
(3) データベースの作成
テストをする場合はProject_testの環境で実施されるのでDBもそれに対応させる必要があります。(db:create:allの指定)
NetBeansで[Rakeタスクを実行/デバッグ..]を選択します。
    フィルタ(F):
    パラメータ(P):
    一致するタスク(M): db:create:all


実行結果
(in D:/Rails_Projects/Test002)

6-2.アプリケーションの作成
(1) scaffoldの利用
NetBeansで[生成..]を選択します。
    ジェネレータ(G): scaffold
    モデル名(N): Record
    属性ペア(フィールド型)(A): cd:string a:string b:string


実行結果
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/records
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/records/index.html.erb
create app/views/records/show.html.erb
create app/views/records/new.html.erb
create app/views/records/edit.html.erb
create app/views/layouts/records.html.erb
create public/stylesheets/scaffold.css
create app/controllers/records_controller.rb
create test/functional/records_controller_test.rb
create app/helpers/records_helper.rb
create test/unit/helpers/records_helper_test.rb
route map.resources :records
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/record.rb
create test/unit/record_test.rb
create test/fixtures/records.yml
create db/migrate
create db/migrate/20100303082714_create_records.rb

(2) マイグレーションファイルの実行
NetBeansで[データベースマイグレション]→[現在のバージョンへ]を選択します。


実行結果
(in D:/Rails_Projects/Test002)
== CreateRecords: migrating ==================================================
-- create_table(:records)
-> 0.0930s
== CreateRecords: migrated (0.0930s) =========================================

6-3.機能の追加
レコードから読み込んだデータaとbを加算し、結果をListに表示するとともに、同じcdの場合はその合計も表示します。また、それぞれの計算結果がマイナスのとき、その数値は赤表示とします。

(1) indexメソッドの修正


/app/controllers/records_controller.rb
class RecordsController < ApplicationController
# GET /records
# GET /records.xml
def index
@records = Record.all
st=0
ed=@records.count-1
for i in st..ed do
stfg="ON"
edfg="ON"
unless i==st then
stfg="off" if @records[i][:cd]==@records[i-1][:cd]
end
unless i==ed then
edfg="off" if @records[i][:cd]==@records[i+1][:cd]
end
kei=0.0 if stfg=="ON"
@records[i][:c]=@records[i][:a].to_f + @records[i][:b].to_f
kei+=@records[i][:c]
@records[i][:d]=nil if edfg=="off"
@records[i][:d]=kei if edfg=="ON"

end

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @records }
end
end
  :
  :
end

(2) ビュープログラムの修正


/app/views/records/index.html.erb
<h1>Listing records</h1>

<table>
<tr>
<th>CD</th>
<th align="right">A</th>
<th align="right">B</th>
<th align="right">A+B</th>
<th align="right">Total</th>
</tr><% @records.each do |record| %>
<tr>
<td><%=h record.cd %></td>
<td align="right"><%=h record.a %></td>
<td align="right"><%=h record.b %></td>
<% if record.c < 0 then %>
<td align="right"><font color="Red"><%=h record.c %></font></td>
<% else %>
<td align="right"><%=h record.c %></td>
<% end %>
<% if record.d then %>
<% if record.d < 0 then %>
<td align="right"><font color="Red"><%=h record.d %></font></td>
<% else %>
<td align="right"><%=h record.d %></td>
<% end %>
<% else %>
<td></td>
<% end %>

<td><%= link_to 'Show', record %></td>
<td%gt;<%= link_to 'Edit', edit_record_path(record) %></td>
<td><%= link_to 'Destroy', record, :confirm => 'Are you sure?', :method => :delete %></td>
</tr><% end %>
</table>

<br /><%= link_to 'New record', new_record_path %>

(3) プログラムの実行
実際のプログラムの動きは次のようになります。

6-4.テストパターンの作成
(1) 0件の場合
テストデータをフィクスチャに登録しないことにより、0件の場合のテストをします。
indexメソッドを呼んでindex.html.rebが表示されればOKとしましょう。


/test/fixtures/records.yml
# nothing




/test/functional/records_controller_test.rb
class RecordsControllerTest < ActionController::TestCase
test "0 cases in records" do
get :index
assert_response :success
assert_not_nil assigns(:records)
end
  :
  :
end

(2) 1件の場合
初期データはないので、プログラムのcreateメソッドを呼んでデータを登録し、テストを行います。


/test/functional/records_controller_test.rb
class RecordsControllerTest < ActionController::TestCase
  :
  :
test "1 case in records" do
post :create, :record=>{:id=>1, :cd=>"aaaa", :a=>"-1", :b=>"-2"}
get :index
assert_response :success
assert_equal -3.0, assigns(:records)[0][:c]
assert_equal -3.0, assigns(:records)[0][:d]
end
  :
  :
end

(3) 2件の場合
初期データはないので、プログラムのcreateメソッドを呼んでデータを登録し、テストを行います。


/test/functional/records_controller_test.rb
class RecordsControllerTest < ActionController::TestCase
  :
  :
test "2 cases in records" do
post :create, :record=>{:id=>2, :cd=>"bbbb", :a=>"2", :b=>"3"}
post :create, :record=>{:id=>3, :cd=>"bbbb", :a=>"-3", :b=>"-4"}
get :index
assert_response :success
assert_equal 5.0, assigns(:records)[0][:c]
assert_nil assigns(:records)[0][:d]
assert_equal -7.0, assigns(:records)[1][:c]
assert_equal -2.0, assigns(:records)[1][:d]
end
  :
  :
end

(4) 3件の場合
初期データはないので、プログラムのcreateメソッドを呼んでデータを登録し、テストを行います。


/test/functional/records_controller_test.rb
class RecordsControllerTest < ActionController::TestCase
  :
  :
test "3 cases in records" do
post :create, :record=>{:id=>4, :cd=>"cccc", :a=>"4", :b=>"5"}
post :create, :record=>{:id=>5, :cd=>"cccc", :a=>"-5", :b=>"-6"}
post :create, :record=>{:id=>6, :cd=>"cccc", :a=>"6", :b=>"7"}
get :index
assert_response :success
assert_equal 9.0, assigns(:records)[0][:c]
assert_nil assigns(:records)[0][:d]
assert_equal -11.0, assigns(:records)[1][:c]
assert_nil assigns(:records)[1][:d]
assert_equal 13.0, assigns(:records)[2][:c]
assert_equal 11.0, assigns(:records)[2][:d]
end
  :
  :
end

(5) マイナス表示のテスト
計算結果がマイナスのとき結果数値を赤表示しているかの検証はhtmlの中に<font color=”Red”>が埋め込まれていることを確認することで行います。


/test/functional/records_controller_test.rb
class RecordsControllerTest < ActionController::TestCase
  :
  :
test "minus in result" do
post :create, :record=>{:id=>7, :cd=>"dddd", :a=>"-7", :b=>"-8"}
get :index
assert_template :index
assert_select "font", 2
end
  :
  :
end

(6) プラス表示のテスト
計算結果がプラスのときは、結果数値は赤表示しません。検証はhtmlの中に<font color=”Red”>が埋め込まれていないことを確認することで行います。


/test/functional/records_controller_test.rb
class RecordsControllerTest < ActionController::TestCase
  :
  :
test "plus in result" do
post :create, :record=>{:id=>8, :cd=>"eeee", :a=>"8", :b=>"9"}
get :index
assert_template :index
assert_select "font", false
end
end

6-5.テストの実行
NetBeansで[テスト]を選択します。


実行結果
Test-unit gem not found, falling back to default test-unit
(in D:/Rails_Projects/Test002)
C:/Ruby/bin/ruby.exe -I"lib;test" -r"C:/Program Files/NetBeans 6.8/ruby2/nb_test_runner.rb"
"C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
"test/unit/helpers/records_helper_test.rb" "test/unit/record_test.rb"
C:/Ruby/bin/ruby.exe -I"lib;test" -r"C:/Program Files/NetBeans 6.8/ruby2/nb_test_runner.rb"
"C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
"test/functional/records_controller_test.rb"
C:/Ruby/bin/ruby.exe -I"lib;test" -r"C:/Program Files/NetBeans 6.8/ruby2/nb_test_runner.rb"
"C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"

1.406 秒で完了しました。
7 個のテスト、0 個の失敗、0 個のエラー

functionalテスト全てのケースで成功となりました。

テストで使うメソッド | index | Ajaxのテスト