nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

Railsのテスト覚書

unit test

  • メソッド単位のテスト
  • test/unit/model_name_test.rb に記述
  • rake test または rake test:units などで実行

functional test

  • アクション単位のテスト
  • test/functional/name_controller_test.rb に記述
  • rake test または rake test:functionals などで実行

example

test "should get index" do
  get :index
  assert_response :success
  assert_not_nil assigns(:posts)
end

リクエストの送信

get
  • get(アクション名, パラメータのハッシュ, セッションのハッシュ, フラッシュのハッシュ)
  • get(:index, {:id => 1}, {:user_id => 10}, {:message => "hello world"})
post
  • post(アクション名, パラメータ)
  • post(:create, :post => {:title => "Hi", :body => "nice to meet you"})

値の取得

accigns
flash
  • flashの値を取得
session
  • セッションの値を取得
cookies
  • クッキーの値を取得

検証

assert_rsponse
  • レスポンスコードの検証