Railsのテスト覚書
参考サイト
http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers
http://railsdoc.com/test
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"})
値の取得
flash
- flashの値を取得
session
- セッションの値を取得
cookies
- クッキーの値を取得
検証
assert_rsponse
- レスポンスコードの検証