webcalener に手を入れてみる (その 2)
試験を作る、と言いつつ違うコトをしている。
view の修正
昨晩、link_to_remote のナニを試していてぐちゃぐちゃに。ちょっと元に戻って落ち着いて機能を実装していった方が良さげ、という事で (そろそろ svn か何かで元に戻せるようにしたいな、と思う今日この頃)。
- 今月に戻るリンクの追加
- before, after のバグ修正と helper メソド化
- todo から CSS パクった
- カレンダの部分を partial なナニに
を盛り込んでみた。
む。link_to_remote 実装の時に、呼ばれるアクションでは partial なナニだけ render すりゃええ、って思ってたら上のリンクも変更しなきゃいけねぇんだ。とほほほ。
link_to_remote で全体を書き換える位だったら今の状態で問題ないのかなぁ。現時点の view の状態は以下。
app/views/calendar/index.rhtml
<h1>Dynamic Calendar Example</h1> <table noborder> <tr><td><%= link_to 'before', { :controller => 'calendar', :action => 'index', :year => beforeYear(@year, @month), :month => beforeMonth(@month)}, :class => 'menu_link' %> </td><td><%= link_to 'HOME', { :controller => 'calendar', :action => 'index', :year => thisYear, :month => thisMonth}, :class => 'menu_link' %> </td><td><%= link_to 'next', { :controller => 'calendar', :action => 'index', :year => nextYear(@year, @month), :month => nextMonth(@month)}, :class => 'menu_link' %> </td></tr> </table> <%= render :partial => 'cal', :locals => { :year => @year, :month => @month, :databinder => @databinder } %>
render なナニは元に戻す可能性大。
試験の実装 (event コントローラ編)
app/models/event.rb は空なんで unit な試験は実装不要。とりあえず event コントローラは scaffold で自動生成なんですが、勉強のためと称して後ヅケで試験を実装してみる。まずは events テーブルの fixture なナニが必要ってコトで適当に作成。
test/fixtures/events.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html first: id: 1 name: test1 description: てすつ 1 startdate: "2006-08-11" another: id: 2 name: test2 description: てすつ 2 startdate: "2006-08-11"
データに日本語入れてみたので config/environment.rb に $KCODE = 'u' も入れておく。で、event コントローラのメソドは
$ grep def app/controllers/event_controller.rb def index def list def show def new def create def edit def update def destroy $
となっているのですが、ソース中に以下のような記述あり。
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list }
コード見るだけで意味が類推できるって素晴しひ。あくまでも類推ですが、HTTP の POST メソドが使えるアクションは destroy、create、update だけで、それ以外は list アクションに redirect されます、という風に読めるな。これも試験してみよう。コードはこんな感じ?? (ちなみにこれはフレームワーク側のナニなので通常は試験の必要は無いモノと思われます)
# ちなみに上記類推は大間違いでした。
def test_verify invalid_action_table = [ :index, :list, :show, :new, :edit ] invalid_action_table.each do |act| post act assert_redirected_to :action => :list end end
って、ソース開いたら試験が一通り書いてあるしー。(とほほほ
とりあえず上記のコードを盛り込んでみて試験をしてみよう。でもこれって機能試験の範疇内でしょうか。なんか微妙だな。
test 用の DB が作成されておらず、試験は失敗。rake にコピーなナニがあったな、という事を思いだし、トライ。以下の手順。
- DB のサーバ側にて test 用 DB の作成及び権限の付与
- 試験機側にて rake clone_structure_to_test の実行
ひぇ〜。簡単スギ。
で、試験してみたんですが、
Expected response to be a <:redirect>, but was <200>
との事。どうも類推が間違っていたようで、上記の verify は GET で :destroy, :create, :update なアクションを呼ぶなよな、という事の模様。(3 日坊主日記 による)
てコトは試験はこんな感じか。
def test_verify invalid_action_table = [ :destroy, :create, :update ] invalid_action_table.each do | act | get act assert_redirected_to :action => :list end end
確かに redirect されている模様。それにしてもこの自動生成なナニは機能試験の雛形として参考になるな。以下にデフォルトのナニを引用しときます。
# 上記の試験は不要なのかやっといた方が良いのか微妙。
test/functional/events_controller_test.rb
require File.dirname(__FILE__) + '/../test_helper' require 'events_controller' # Re-raise errors caught by the controller. class EventsController; def rescue_action(e) raise e end; end class EventsControllerTest < Test::Unit::TestCase fixtures :events def setup @controller = EventsController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @first_id = events(:first).id end def test_index get :index assert_response :success assert_template 'list' end def test_list get :list assert_response :success assert_template 'list' assert_not_nil assigns(:events) end def test_show get :show, :id => @first_id assert_response :success assert_template 'show' assert_not_nil assigns(:event) assert assigns(:event).valid? end def test_new get :new assert_response :success assert_template 'new' assert_not_nil assigns(:event) end def test_create num_events = Event.count post :create, :event => {} assert_response :redirect assert_redirected_to :action => 'list' assert_equal num_events + 1, Event.count end def test_edit get :edit, :id => @first_id assert_response :success assert_template 'edit' assert_not_nil assigns(:event) assert assigns(:event).valid? end def test_update post :update, :id => @first_id assert_response :redirect assert_redirected_to :action => 'show', :id => @first_id end def test_destroy assert_nothing_raised { Event.find(@first_id) } post :destroy, :id => @first_id assert_response :redirect assert_redirected_to :action => 'list' assert_raise(ActiveRecord::RecordNotFound) { Event.find(@first_id) } end end
てか、assert assigns(:event).valid? って何?
何を基準に valid 判定してるのか謎だ。調査必要ってコトで、とりあえずスルー。
試験の実装 (calendar コントローラ編)
calendar コントローラ用のナニは当たり前ですが、test_truth のみ。アクションは現時点では一つだけ。基本的には年と月が渡されてテンプレは index を使用。テンプレートに渡すナニは year と month と databinder なんですが、databinder は手続きになっている。これってどうやって確認すんだ? not_nil だったら OK ってコトにしようかなぁ。 (弱
def test_index get :index, :year => 2006, :month => 8 assert_response :success assert_template 'index' assert_not_nil assigns(:year) assert_not_nil assigns(:month) assert_not_nil assigns(:databinder) end
こんな感じッスか?? どーせ間違ってるんだろな。
一応試験は通った。そろそろハードルが高くなってきたな。