Railsにはデフォルトで自動テストがいろいろあるんだねー
rake db:migrate rake db:test:load
rake test:units TEST='test/unit/book_test.rb'
rake test:units
rake test
#coding: utf-8 require 'test_helper' class BookTest < ActiveSupport::TestCase test 'book save' do book = Book.new({ :isbn => '111-2-3333-4444-5', :title => 'テストの本!', :price => 100, :publish => 'テスト出版社', :published => '2012-01-01', :cd => false }) assert book.save, 'Suuccess to save' end end
test '日本語、行けるかな?' do assert true end
test 'book validate' do book = Book.new({ :isbn => '111-2-3333-44', :title => '', :price => 'abc', :publish => '', :published => '2000-01-01', :cd => false }) assert !book.save assert_equal book.errors[:isbn], ['is the wrong length (should be 17 characters)'] assert_equal book.errors.size, 3 # 全体で3件のエラー assert book.errors[:title].any? # titleで何かしらのエラー end
test 'book where method test' do result = Book.where(:title => 'JavaScript本格入門').first assert_instance_of Book, result # resultが、Bookオブジェクトか? assert_equal books(:js).isbn, result.isbn # books.ymlの:jsキーの値と、resultのjsが同じか? assert_equal Date.new(2010,11,26), result.published # result.publishが、2010/11/26か? end
require 'test_helper' class HelloControllerTest < ActionController::TestCase test 'list action' do get :list # これで、HTTP GETでlistアクションを実行 len = assigns(:books).length # コントローラで設定された:booksの件数を取得 assert_equal 10, len # :booksの件数が10件であること assert_response :success # レスポンスコードが、200であること assert_template 'hello/list' # 'hello/list'のテンプレートを使うこと end end
rake test:functionals TEST='test/functional/hello_controller_test.rb'
test 'diff check' do assert_difference 'Book.count', 1 do post :create, :book => { :isbn => '111-2-3333-4444-5', :title => 'テストの本!', :price => 100, :publish => 'テスト出版社', :published => '2012-01-01', :cd => false } end end
test 'routing check' do assert_generates 'hello/list', {:controller => 'hello', :action => 'list'} end
test 'select check' do get :list # これで、HTTP GETでlistアクションを実行 assert_select 'title' # (1)<title>タグがあるか? assert_select 'title', true # (2)上と同じ assert_select 'font', false # (3)<font>が無いか? assert_select 'title', 'Railbook' # (4)<title>下のテキストが「Railbook」か? assert_select 'title', /[A-Za-z0-9]/ # (5)<title>下のテキストが英数字か? assert_select 'script[type=?]', /.+/ # (6)<script>のtypeが何かあるか? assert_select 'table tr[style]', 10 # (7)<table>のstyleのある<tr>が、10個あるか? assert_select 'table' do # (8)<table>のstyleのある<tr>が、1~10まであるか? assert_select 'tr[style]', 1..10 end assert_select 'title', {:count => 1, :text => 'Railbook'} # (9) <title>が1つで、'Railbook'か? end
rails g integration_test admin_login
test 'login test' do # /hello/viewを GETでアクセス get '/hello/view' assert_response :redirect # レスポンスが、リダイレクトであること assert_redirected_to '/login' # リダイレクト先が、/loginであること assert_equal '/hello/view', flash[:referer] # フラッシュの:refererに、現在のURL'/hello/view'が格納されている事 # 直前のリダイレクトのリクエストを処理→リダイレクトしてログイン画面へ follow_redirect! assert_response :success # レスポンスが成功であること assert_equal '/hello/view', flash[:referer] # フラッシュの:refererに、遷移先の'/hello/view'が格納されている事 # ユーザ/パスワードを入れて、ログイン put '/login/auth', {:username => 'abc', :password => 'cdf', :referer => '/hello/view'} assert_response :redirect # レスポンスが、リダイレクトであること assert_redirected_to :controller => 'hello', :action => 'view' # アクションがhello#viewであること # 直前のリダイレクトのリクエストを処理→リダイレクトして/hello/viewへ follow_redirect! assert_response :success # レスポンスが成功であること end
rake test:integration TEST='test/integration/admin_login_test.rb'