RubricksでTest::Unit
Modelのテスト
ソースコード的には一般的なTest::Unit(Rails1.2系)と変わらない。
親クラスは、Rails2.0じゃないのでActionSupport::TestCaseではなくTest::Unit::TestCaseになる。
あと、ポイントとしては、Rubricksで提供(カスタマイズ?)しているtest_helperをrequireすること。
これをしないとcomponentの方を見に行ってくれない模様。
dt_pjdata_project_test.rb
require File.dirname(__FILE__) + '/../test_helper' #<--コレ class DtPjdataProjectTest < Test::Unit::TestCase def setup @pj = DtPjdata::DtPjdataProject.new @pj.man_month_operation_day = 20 @pj.man_day_operation_hour = 8 @pj.description = "description" end # project_nameが無しまたは0文字の場合、バリデーションNGになること def test_validates_length_of_project_name_0_notvalid assert !@pj.valid? @pj.project_name = "" assert !@pj.valid? end :
実行結果
C:\work\NetBeans65\bizca>ruby test\unit\dt_pjdata\dt_pjdata_project_test.rb Loaded suite test/unit/dt_pjdata/dt_pjdata_project_test Started . DtPjdataProjectTest =========================================================== ...... Finished in 0.14 seconds. 7 tests, 5 assertions, 0 failures, 0 errors
NetBeans上からも実行できる。TestCaseのソースコードエディタ上で右クリックして「ファイルをテスト」を選択。
下に実行結果が表示される。コンソールから実行した場合と違って、成功した場合でもどのテストを実行したかがわかる。
あと、テスト結果のところからソースコードに飛べる。(ま、EclipseでJavaとかだと当たり前な世界ですがww)
コンソールから実行した際に実行したテスト名(メソッド名)を出力するようにする場合、puts self.method_nameなどを入れる必要がある。(→ウザイ)
# project_nameが1文字の場合、バリデーションOKになること def test_validates_length_of_project_name_1_valid puts self.method_name @pj.project_name = "1" assert @pj.valid? end # project_nameが50文字の場合、バリデーションOKになること def test_validates_length_of_project_name_50_valid puts self.method_name @pj.project_name = "12345678901234567890123456789012345678901234567890" assert @pj.valid? end
DB更新のテスト。pj.total_man_month_costが更新されていることを確認。fixtureがうまくうごかなかったので初期化処理をこの中でやってます。
# 総工数(人月)を、DtsPjdataBasicDataの合計から算出して更新していること def test_update_total_man_month_cost puts self.method_name pj = DtsPjdata::DtsPjdataProject.find(1) # 事前に一旦総工数(人月)を0にする pj.total_man_month_cost = 0 pj.save! assert_equal(0, pj.total_man_month_cost) puts ' before: ' + pj.total_man_month_cost.to_s # 期待値を算出 工数(人日)の合計 と 1人月あたり人日 total_man_days_cost = DtsPjdata::DtsPjdataBasicData.sum(:man_days_cost_performance, :conditions => ['dts_pjdata_project_id = ?', pj.id]) puts ' expected: ' + total_man_days_cost.to_s + ' / ' + pj.man_month_operation_day.to_s expected_total_man_month_cost = total_man_days_cost / pj.man_month_operation_day # メソッド実行 & 検証 pj.update_total_man_month_cost assert_equal(expected_total_man_month_cost, pj.total_man_month_cost) puts ' after: ' + pj.total_man_month_cost.to_s end
実行結果
DtsPjdataProjectTest =========================================================== .test_update_total_man_month_cost before: 0.0 expected: 90.0 / 20 after: 4.5 .test_validates_length_of_project_name_0_notvalid .test_validates_length_of_project_name_1_valid .test_validates_length_of_project_name_50_valid .test_validates_length_of_project_name_51_notvalid . Finished in 0.515 seconds. 7 tests, 7 assertions, 0 failures, 0 errors
Controllerのテスト
基本的にはModelと同じ。setupでリクエストやレスポンスを作る。あと、sessionに[:user]でユーザIDを保持させておくことでログイン状態にすることができる(必須w)。
require File.dirname(__FILE__) + '/../test_helper' class ProjectConfigControllerTest < Test::Unit::TestCase #fixtures :dt_pjdata_configs def setup @controller = DtPjdata::ProjectConfigController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @request.env["HTTP_REFERER"] = '/' #ログイン状態にする @request.session[:user] = 1 end # showにてdt_pjdata_configsに値があるとき、@help_urlを取れること def test_show_normal # アクションを実行 get :show assert_response :success # idが"dtpjdata_config_comment_edit_button"のspanがあること assert_select "span#dtpjdata_config_comment_edit_button" # コントローラ内の@help_urlを取得する created_help_url = assigns("help_url") assert_equal("http://www.google.com", created_help_url) end