本当にあった怖い話7つの幽霊 7つの成仏
2015/09/03(木) 表参道.rb #4
コードに宿った幽霊・心霊現象
幽霊・心霊現象の紹介
1. 皿屋敷 - お菊の亡霊
2. ドッペルゲンガー
3. ポルターガイスト
4. 既視感 - デジャヴ
5. 日本人形 - 髪が伸びる人形
6. 九死に一生
7. アナフィラキシーショック
1. 皿屋敷 - お菊さん
ある日、何気なくCIサーバーの結果を眺めていると・・・
1. 皿屋敷 - お菊さん
describe ‘テスト’ do let!(:my_friends) { create_list :friend, 10, with: current_user } let!(:other_friends) { create_list :friend, 10, with: other_user } let!(:named_friends) { create_list :friend, 10, with: named_user }
it ‘テスト’ do # do something end ...end
1人、2人・・・友だちが1人足りない・・・
除霊
describe ‘テスト’ do let!(:my_friends) { create_list :friend, 2, with: current_user } let!(:other_friends) { create_list :friend, 2, with: other_user } let!(:named_friends) { create_list :friend, 2, with: named_user } before { allow(User).to receive(:default_per_page) { 1 } }
it ‘テスト’ do # do something end ...end
10人も友だちは要らない
2.ドッペルゲンガー
とあるクラスに、first_image, second_image, third_image, fourth_image というカラムがいました。
そして、それぞれ画像アップロード用のクラスで FirstImageUploader, SecondImageUploader, (ry がいまし
た。
Uploaderのコードをdiffしてみた
$ diff app/uploaders/first_image_uploader.rb app/uploaders/second_image_uploader.rb2c2< class FirstImageUploader < CarrierWave::Uploader::Base---> class SecondImageUploader < CarrierWave::Uploader::Base$
約50行が全く同じファイルが4つもあった!
除霊
class CompanyImageUploader < CarrierWave::Uploader::Base # 共通処理
end
class FirstImageUploader < CompanyImageUploaderend
class SecondImageUploader < CompanyImageUploaderend
リファクタリング: 親クラスの抽出
3.ポルターガイスト
誰も触っていないはずのページが、心霊現象によってカラム落ち
していた。
除霊
width を指定は最低限にして、出来るだけbootstrapのグリッドレ
イアウトなど相対指定をしましょう
4.既視感 - デジャヴ
とあるviewにいたコード
%p= raw obj.html_safe
「raw と html_safe の違いは何だろう・・・?」
4.既視感 - デジャヴ
除霊
Slackで報告したら、弊社の @yachibit が git grep 'raw.*html_safe' と sed を組み合わせて、一斉除霊したPRを出してく
れた。
参考: html_safe、raw、「<%==」の比較
5.日本人形 - 髪の伸びる人形
localeファイルに追記しようと、ファイルを開くと・・・
5.日本人形 - 髪の伸びる人形
localeファイルに追記しようと、ファイルを開くと・・・
1ファイルが2,200行を超えていた
除霊
localeファイルを分割した。(参考: RailsGuides)
6.九死に一生
CIサーバーのテストが稀によく落ちる。
リトライすると success になるけど、怖いので調査してみた。
6.九死に一生
feature ‘テスト’ do let(:old_sort_order) { Faker::Number.number(1) }
scenario ‘テスト’ do # ソート順を変更する処理
expect(page).to_not have_content old_sort_order endend
6.九死に一生
feature ‘テスト’ do let(:old_sort_order) { Faker::Number.number(1) }
scenario ‘テスト’ do # ソート順を変更する処理
expect(page).to_not have_content old_sort_order endend
1/10 で1が出ると、画面内にある +1 の文字と・・・
除霊
feature ‘テスト’ do let(:old_sort_order) { 1 }
scenario ‘テスト’ do # ソート順を変更する処理
within ‘.content’ do expect(page).to_not have_content old_sort_order end endend
7.アナフィラキシーショック
とあるページをリロードすると、2回目の表示はエラーが発生した。
7.アナフィラキシーショック
GET でアクセスするページなのに、 session からデータを消す処
理があり、2回目はエラーに・・・
除霊
できるだけ RESTful にしましょう。
あと、リソースのネストを活用したりもしましょう。
Rails.application.routes.draw do resources :jobs do # app/controllers/jobs/companies_controller.rb resource :company, module: :jobs endend
7つの幽霊を成仏させました
次の幽霊が現れないことを祈っています
まとめ
● 怪しい気配がしたら、git grepする
● 手に負えなくなる前に除霊する
● Faker (乱数)は用法・用量を守って使う
ご清聴ありがとうございました