毎回やってることのメモ.
作業のリポジトリはこちら
方針
- 不要な自動生成コメントは削除
- 作業は issue 単位で行う. 変更は Pull-Request にして, build が通ったら Merge する
- いずれ必要になりそうなものでもいま必要なければコメントアウトではなく削除する
Ruby のインストール
rbenv で任意の Ruby のバージョンを入れる. 2.1.3 を使う.
rbenv install 2.1.3
rbenv local 2.1.3
rbenv rehash
Rails のインストール
今回は 4.1.7 をインストール.
gem install rails -v 4.1.7
アプリケーションのひな形の作成
ここまできたらリポジトリを作る.
ghq でリポジトリ管理しているので,
ghq get {ユーザー名}/{リポジトリ名}
でローカルにリポジトリと対応するディレクトリ作る.
cd $(ghq list -p | peco)
とかで peco と cd コマンドでディレクトリ移動.
一つ上の階層行って作る
ghq get sqrtxx/kurobako
cd $(ghq list -p | peco) # kurobako ディレクトリへ
cd ..
rails new kurobako -T
-T
は --skip-test-unit
の略. RSpec 使うのでスキップ.
それで作成されたファイルを git で管理. rails new
で .gitignore
も生成されているので, そのまま追加でいいと思う.
git add .
git commit -m 'Initial Commit'
Gem などの整理
以降は実際の issue なども参照しながら.
Gemfile の整理
Gemfile を整理する · Issue #2 · sqrtxx/Kurobako
不要なコメントなどの削除
Readme の編集
Readme を編集する · Issue #3 · sqrtxx/Kurobako
rdoc を md にして, 不必要な文言を削除.
テスト環境の整理
テスト環境を整理する · Issue #4 · sqrtxx/Kurobako
Spring を入れる
rails/spring を入れる. まず Gemfile
に追加
gem 'spring', group: :development
bundle install
してから以下のコマンド
bundle exec spring binstub --all
今回は既に入っていたためスキップ.
spring がおかしくなったら以下を試す.
Rails - spring付きのrspec実行が、cannot load such fileエラーで落ちる場合の対処法 - Qiita
RSpec を入れる
Gemfile
に以下を追加
group :development do
gem 'spring' # group 内に移動
gem 'spring-commands-rspec' # spring で rspec を使うための gem
gem 'better_errors' # エラーを綺麗に表示するための gem
gem 'rails_best_practices' # rails の best practice を表示するための gem
end
group :development, :test do
gem 'rspec-rails', '~> 2.14.0' # rspec 本体
gem 'factory_girl_rails', '~> 4.2.1' # テスト用ファクトリを作るための gem
end
group :test do
gem 'faker', '~> 1.1.2' # テスト用のダミーデータを作るための gem
gem 'database_cleaner', '~> 1.0.1' # データベースをテスト毎に綺麗にするための gem
end
bundle install
rails generate rspec:install
.rspec
に format 指定を入れる
--color
--format documentation
テストしてみる
$ bundle exec rspec spec
No examples found.
Finished in 0.00013 seconds
0 examples, 0 failures
正しく動いている.
spring と direnv を使って Rails と rspec を高速起動。快適開発はじめる - けんごのお屋敷
Guard を入れる
bin ファイルを作る
bundle binstub guard
Gemfile
に以下を追加
group :development do
gem 'guard-rspec'
gem 'guard-shell'
end
Guardfile
に以下を指定.
guard 'shell' do
watch(%r{^app/(.+)(\.rb|\.erb|\.haml|\.slim|\.rabl)$}) {|m| `rails_best_practices #{m[0]}` }
end
guard 'rspec', cmd: 'spring rspec -f doc', all_on_start: false, all_after_pass: false do
watch(%r{^spec/.+_spec\.rb$})
watch('spec/spec_helper.rb') { 'spec' }
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^config/initializers/(.+)\.rb$}) { |m| "spec/initializers/#{m[1]}_spec.rb" }
watch(%r{^spec/factories/(.+)\.rb$}) { 'spec/factories_spec.rb' }
watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
watch('config/routes.rb') { 'spec/routing' }
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
end
実行してみる
$ guard
12:19:37 - INFO - Guard is using Emacs to send notifications.
12:19:37 - INFO - Guard is using TerminalTitle to send notifications.
12:19:37 - INFO - Guard::RSpec is running
12:19:37 - INFO - Guard is now watching at '/Users/sqrtxx/wip/src/github.com/sqrtxx/kurobako'
[1] guard(main)>
12:19:38 - INFO - Run all
Source Codes: |==================================================================================================================================================================|
Please go to http://rails-bestpractices.com to see more useful Rails Best Practices.
No warning found. Cool!
Source Codes: |==================================================================================================================================================================|
Please go to http://rails-bestpractices.com to see more useful Rails Best Practices.
No warning found. Cool!
Source Codes: |==================================================================================================================================================================|
Please go to http://rails-bestpractices.com to see more useful Rails Best Practices.
No warning found. Cool!
12:19:40 - INFO - Running all specs
No examples found.
Finished in 0.00018 seconds
0 examples, 0 failures
Travis CI に対応する
language: ruby
rvm:
- 2.1.3
https://travis-ci.org/ でサインインし, https://travis-ci.org/profile/{アカウント名}
にアクセスして ON にする
テストが通ると pull-request ページの merge ボタンが以下のような表示となる.
バッジを追加する
[](https://travis-ci.org/sqrtxx/Kurobako)
上の条件の場合, master ブランチで build が通れば, 以下のような表示となる.
ジェネレータで生成されるファイルの抑制をする
config/application.rb
を以下のように変更. ついでに不要なコメントを削除
module Kurobako
class Application < Rails::Application
config.generators do |g|
g.test_framework :rspec,
fixtures: true,
view_specs: false,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: false
g.fixture_replacement :factory_girl, dir: "spec/factories"
end
end
end
FactoryGirl を include しておく
毎回 FactoryGirl.create
など書くのはだるいので Method を include しておく.
spec/spec_helper.rb
を編集. before(:suite)
において reload しないと FactoryGirl の変更が反映されない.
RSpec.configure do |config|
config.before(:suite) do
FactoryGirl.reload
end
config.include FactoryGirl::Syntax::Methods
end
Spring compatibility · Issue #120 · thoughtbot/factory_girl_rails
データベースクリーナーの設定を追加する
spec/spec_helper.rb
を編集.
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end