概要
勉強の一環として、備忘録を残します。 誤った情報を発信している可能性も十二分にございますので、 間違いを発見された方はご指摘いただけますと幸いです。この章に取り組む前にやった事
- Docker(webコンテナ)にRSpecを導入
なぜRSpecを導入しようと思ったのか
・他のテストフレームワークと比較して情報量が多かったから。Aの情報量が多い→Aを使っているエンジニアの数が多い→Aを採用している企業が多いと考えた。
という事で、下記の条件で調べてみました。
- Github:「ruby language:Ruby テストフレームワーク名」でヒットしたリポジトリ数
- Qiita:「テストフレームワーク名 tag:rails」でヒットした記事数
テストフレームワーク | Github | Qiita |
---|---|---|
RSpec | 約3300 | 3195 |
Minitest | 300 | 547 |
※2023年6月11日時点の結果です。
3章でやる事(ざっくり)
- HTMLファイルだけで構成されている静的なページの作成
- 自動化テストの雰囲気を掴む(この作業をRSpecでやってみる)
自動化テスト
TDD(テスト駆動開発)のフロー①失敗するテストを書く
②テストをパスする最小限のコードを書く
③コードの重複を除去する(リファクタリング)
後に登場する主要なテスト3つ
- コントローラーテスト
- モデルテスト
- 統合テスト
テストの概要についてはこちらの記事が分かりやすかった。
Rails チュートリアル 【初心者向け】 テストを10分でおさらいしよう!
RSpecの導入
・Gemの追加&bundle installGemfile
group :development, :test do
gem 'rspec-rails'
end
・RSpecの初期設定(webコンテナを起動した状態で)
$ docker-compose exec web rails g rspec:install
最初のテスト(3.3.1)をRSpecで実行する
Static_Pages_Controllerのデフォルトテストコード。static_pages_controller_test.rb
require "test_helper"
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
end
test "should get help" do
get static_pages_help_url
assert_response :success
end
end
これをRSpec仕様に書き換える。
static_pages_controller_spec.rb
require "rails_helper"
RSpec.describe "StaticPagesController", type: :request do
describe "Get /home" do
it "returns http success" do
get static_pages_home_url
expect(response).to have_http_status(:success)#"response"はコントローラーアクションの結果として返されたレスポンスオブジェクトを表す
end
end
describe "GET /help" do
it "returns http success" do
get static_pages_help_url
expect(response).to have_http_status(:success)
end
end
テストを走らせる。
$ docker-compose exec web bundle exec rspec spec/controllers/static_pages_
controller_spec.rb
実行結果
Finished in 0.98563 seconds (files took 4.98 seconds to load)
2 examples, 0 failures
次に、失敗するテストを意図的に書いてみる。
static_pages_controller_spec.rb
require "rails_helper"
RSpec.describe "StaticPagesController", type: :request do
describe "Get /home" do
it "returns http success" do
get static_pages_home_url
expect(response).to have_http_status(:success)#"response"はコントローラーアクションの結果として返されたレスポンスオブジェクトを表す
end
end
describe "GET /help" do
it "returns http success" do
get static_pages_help_url
expect(response).to have_http_status(:success)
end
end
describe "GET /about" do
it "returns http success" do
get static_pages_about_url
expect(response).to have_http_status(:success)
end
end
テストを走らせる。
$ docker-compose exec web bundle exec rspec spec/controllers/static_pages_
controller_spec.rb
実行結果
Failures:
1) StaticPagesController GET /about returns http success
Failure/Error: get static_pages_about_url
NameError:
undefined local variable or method `static_pages_about_url' for #<RSpec::ExampleGroups::StaticPagesController::GETAbout:0x000055c7d796eb10>
# ./spec/controllers/static_pages_controller_spec.rb:34:in `block (3 levels) in <top (required)>'
Finished in 0.73486 seconds (files took 5.09 seconds to load)
3 examples, 1 failure
Failed examples:
rspec ./spec/controllers/static_pages_controller_spec.rb:33 # StaticPagesController GET /about returns http success
コントローラーのGET/aboutアクションに対するテストが失敗したことを示している。
ルーティングを修正し、再度テストを走らせる。
routes.rb
Rails.application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
end
実行結果
Finished in 0.86823 seconds (files took 5.35 seconds to load)
3 examples, 0 failures
テストがパスしたので、リファクタリングを行える状態になった。
なぜリファクタリングが必要か
長期的な視点で開発効率と品質向上を図る為。コードの保守性、可読性、再利用性、バグ修正、パフォーマンス等の観点から、定期的にリファクタリングを行う事が推奨される。(chatGBTさんより)
未来の自分や、コードを読む他のエンジニアの方々への配慮が大事。