0
0

More than 1 year has passed since last update.

TDDで機能追加するとき

Posted at

overview

  • Railsのアプリに機能を追加したり編集したり削除したりするときの自己流フロー
  • 備忘録
  • TDDでやりんす(Minitest)

Get staretd

Issue作成

  1. githubでissueを作る
    1. チェックボックスでテストメソッドのタイトルを書いておくと良いかも

テストを作る際には以下のルールを設けています
・基本的にModelごとにcreate, read, update, deleteの4ファイルで構成される
・各テストファイルのテスト名はUnable to 〜, Able to 〜と2種類の文頭で始まる
・Unable to 〜でNGなリストを作ることでテストファイルを仕様書として機能させます

# example

- [ ] Create
   - Unable to create without Login
   - Unable to create with blank
   - Able to create
- [ ] Read
- [ ] Update
- [ ] Delete
   - Unable to delete without Login
   - Unable to delete other users tweet
   - Able to delete

作業ブランチ作成

ブランチの運用ルールは以下の通りです
main => 本番環境用のブランチ
dev => 最新の開発用ブランチでここから作業ブランチを作る(mainへのmergeは管理者のみ)
f/xxx => 作業用ブランチで必ずDevブランチから派生する

  1. $ git fetch
  2. $ git status
  3. $ git branch -a
  4. $ git checkout -b f/add-tweet-model origin/dev

テストファイル作成

  1. $ rails t
  2. CRUDで4つのファイルを作成(tweetディレクトリの中で作成することでメンテナンス性up)
    1. $ rails g integration_test tweet/create
    2. $ rails g integration_test tweet/read
    3. $ rails g integration_test tweet/update
    4. $ rails g integration_test tweet/delete
  3. $ rails t(greenであればok)

Red, Green, Refactoring

# test/integration/tweet/create_test.rb

def
  def setup
    @alice = users(:alice)
  end
  test 'Unable to create without Login' do
    sign_in(@alice)
    assert_no_difference 'Tweet.count' do
      post tweet_path, params: { tweet: {
        title: 'hello',
        content: 'world!',
      } }
    end
    assert_response :redirect
    follow_redirect!
    assert_equal tweets_path, request.path
  end
end

PR発行

  1. $ git status
  2. $ git add .
  3. $ git commit -m'Add Test Suite for Tweet'
  4. $ git push origin HEAD
  5. ブラウザからPR発行
    1. f/add-tweet-model => dev
    2. dev => main

Heroku Deploy

  1. $ git checkout main
  2. $ git status
  3. git pull
  4. rails t
  5. git push heroku
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0