SlideShare a Scribd company logo
1 of 36
Download to read offline
Guard Rspec + Spork による自動テスト




                          小崎玄顕

                         平成23年10月1日(土)




KOZAKI Tsuneaki                           Slippy on github
Blog Forever Undecided                   Twitter @koko_u
Guard Rspec + Spork による自動テスト
   自己紹介
       名前 : 小崎 玄顕 (こざき つ ねあき )
                       こざき
       つ いっ た : @koko_u
       職業 : SIer, COBOL-er, Excel-er, いろいろ …
       趣味 : 読書 (読む だ け )、音楽 (聞く だ け )、プログ ラ ミン グ (パク るだ け )
                  読む       、音楽 聞く           、プログ       パク
       興味 (soft): Ruby, Rails, Scala, Play!, ...
       興味 (hard): Sony Reader, Android, ThinkPad, Happy Hacking
       Keyboard, ...




KOZAKI Tsuneaki                                           Slippy on github
Blog Forever Undecided                                   Twitter @koko_u
Guard Rspec + Spork による自動テスト
   今日のお題
       自動テス ト の環境と して Guard + Spork を 紹介します
       実際に簡単な サン プル プロジ ェク ト を 作ります




KOZAKI Tsuneaki                                Slippy on github
Blog Forever Undecided                        Twitter @koko_u
Guard Rspec + Spork による自動テスト
   アンケート
        guard を 知っ ている?
        spork を 知っ ている?




KOZAKI Tsuneaki              Slippy on github
Blog Forever Undecided      Twitter @koko_u
Guard Rspec + Spork による自動テスト
   そもそもなぜテストが必要なのか?




KOZAKI Tsuneaki              Slippy on github
Blog Forever Undecided      Twitter @koko_u
Guard Rspec + Spork による自動テスト
   実行し ないと正し いこ とを確かめられないからですね




KOZAKI Tsuneaki                   Slippy on github
Blog Forever Undecided           Twitter @koko_u
Guard Rspec + Spork による自動テスト
   例えば ...
   01 # wrong_hello.rb
   02 def wrong_hello(name)
   03  if name.nil?
   04    puts "you are nil boy"
   05  elseif name.empty?
   06    puts "you are empty girl"
   07  else
   08    puts "hello #{name}"
   09  end
   10 end




KOZAKI Tsuneaki                       Slippy on github
Blog Forever Undecided               Twitter @koko_u
Guard Rspec + Spork による自動テスト
   普段は何ごともなく動く
    1 require './wrong_hello'
    2 wrong_hello 'koko_u'
    3
    4 # => hello koko_u




KOZAKI Tsuneaki                  Slippy on github
Blog Forever Undecided          Twitter @koko_u
Guard Rspec + Spork による自動テスト
   でも実は間違えている
    1 require './wrong_hello'
    2 wrong_hello ''
    3
    4 # => hello

   ...あれ? empty girl は?




KOZAKI Tsuneaki                  Slippy on github
Blog Forever Undecided          Twitter @koko_u
Guard Rspec + Spork による自動テスト
   すごく間違えてる ...
    1 require './wrong_hello'
    2 wrong_hello nil
    3
    4 # => `wrong_hello': undefined method `empty?' for nil:NilClass
      (NoMethodError)

   例外が飛んできたよ!




KOZAKI Tsuneaki                                                         Slippy on github
Blog Forever Undecided                                                 Twitter @koko_u
Guard Rspec + Spork による自動テスト
   ささいな typo でもその行が 実行される までエラーが発見できな
             でも その行が実行される
   い
   テストする し かないですね




KOZAKI Tsuneaki                  Slippy on github
Blog Forever Undecided          Twitter @koko_u
Guard Rspec + Spork による自動テスト
   なぜ Guard + Spork なのか?
   なるべくストレスなくテストを実行したい
   テスト結果が notify などで表示されると、なんとなくデキる気がする

   他の選択肢は
   autotest (Zentest)
    よくわからなかった
   watchr
    新規にファイルを追加した時にうまく動かなかった




KOZAKI Tsuneaki                            Slippy on github
Blog Forever Undecided                    Twitter @koko_u
Guard Rspec + Spork による自動テスト
   デモ
       実際に guard と spork が 動いている所を まず見せ ます
         1. デモプロジェクトの直下で guard を起動する と、まずは全
            てのテストが実行されます
         2. その後、テストファイルもし くはソースを変更する 度に対応
            する テストが実行されます




KOZAKI Tsuneaki                               Slippy on github
Blog Forever Undecided                       Twitter @koko_u
Guard Rspec + Spork による自動テスト
   Spork の利点
       自動的にテス ト を 実行しているのは Guard の機能です
       た だ 、ご 存知のよう に Rails のテス ト の実行早く ありませ ん。
       自動テス ト で頻繁にテス ト を 実行す る時、「 す ぐ に」 結果が 返っ てこな い
       と イ ラ イ ラ してしまう
       そこで、 Spork が あら か じ め Rails の環境ロー ド までを 済ませ ておいて、
       リ ク エス ト に応じ てテス ト を 実行します

   どのくらい実行時間に差がある かも確認し てみまし ょう




KOZAKI Tsuneaki                                   Slippy on github
Blog Forever Undecided                           Twitter @koko_u
Guard Rspec + Spork による自動テスト
   能書きが長すぎた。実際にやってみまし ょう




KOZAKI Tsuneaki              Slippy on github
Blog Forever Undecided      Twitter @koko_u
Guard Rspec + Spork による自動テスト
   前提とする 環境
        OS : Ubuntu 11.04
        Ruby : 1.9.2-p180
        Rails : 3.1.0
        Testing Framework : RSpec
       Mac の人は 自力で頑張っ て下さい。多分 growl に使う gem が 違う く ら
       い?

        Windows の人は ご めんな さい。
       Rails 3.0.x でも動く は ず。せ っ か く リ リ ー ス され た ので 3.1.0 でやります
       が 、 3.1の新機能は 一切でてきませ ん。
              の新機能は
       Testing Framework は RSpec。 Test::Unit でも gem を 差し替えるな ど
                                。
       す れ ばできるは ずです が 、時間が な く てフ ォ ロー できませ んでした 。




KOZAKI Tsuneaki                                         Slippy on github
Blog Forever Undecided                                 Twitter @koko_u
Guard Rspec + Spork による自動テスト
   お品書き
    1. サン プル の Rails プロジ ェク ト を 作成します (6ペー ジ )
                                        ペー
    2. Spork でテス ト を 素早く 実行できるよう 設定します (4ペー ジ )
                                             ペー
    3. Guard を 使っ てテス ト が 自動的に実行され るよう 設定します (4ペー ジ )
                                                ペー




KOZAKI Tsuneaki                                 Slippy on github
Blog Forever Undecided                         Twitter @koko_u
Guard Rspec + Spork による自動テスト
   サンプルとする Rails プロジェクトを作成し ます(1/6)
                            ます
   $ rails new guard_demo -T --skip-bundle
         create
         create README
         create Rakefile
         create config.ru
         create .gitignore
         create Gemfile
         create app
   ...

       Testing Framework を デ フ ォ ル ト の Test::Unit では な く RSpec に差し
       替えるので、 -T オプション で test デ ィレ ク ト リ の作成を 抑止しています
       Rails 3.1.0 か ら 自動的に bundle install され てしまう ので、これ も抑止
       しています 。
       Gemfile を 修正して bundle install す るので、このタイ ミン グ です ること
       な いんじ ゃ ?




KOZAKI Tsuneaki                                           Slippy on github
Blog Forever Undecided                                   Twitter @koko_u
Guard Rspec + Spork による自動テスト
   サンプルとする Rails プロジェクトを作成し ます(2/6)
                            ます
   Gemfile の最後に下記のコードを追加
    1 group :test, :development do
    2  gem 'rspec-rails'
    3  gem 'spork', '~> 0.9.0.rc'
    4  gem 'guard-rspec'
    5  gem 'guard-spork'
    6  gem 'rb-inotify'
    7  gem 'libnotify'
    8 end

   spork は 0.9.0.rc 以上でないと Rails 3.0 以降で動かないようです
   Mac をお使いの人は最後の 2行を下記のように変更すればいいらしい
    1 group :test, :development do
    2  gem 'rspec-rails'
    3  gem 'spork', '~> 0.9.0.rc'
    4  gem 'guard-rspec'
    5  gem 'guard-spork'
    6  gem 'rb-fsevent'
    7  gem 'growl_notify'
    8 end
KOZAKI Tsuneaki                                Slippy on github
Blog Forever Undecided                        Twitter @koko_u
Guard Rspec + Spork による自動テスト
   サンプルとする Rails プロジェクトを作成し ます(3/6)
                            ます
   やっと bundle install します
   $ bundle install --path=vendor/bundle
   Fetching source index for http://rubygems.org/
   Installing rake (0.9.2)
   Installing multi_json (1.0.3)
   Installing activesupport (3.1.0)
   Installing bcrypt-ruby (3.0.0) with native extensions
   Installing builder (3.0.0)
   Installing i18n (0.6.0)
   Installing activemodel (3.1.0)
   ...

       ここで --path オプション を 付け な いと 、最初に --skip-bundle した 意味
       が な く な るよ




KOZAKI Tsuneaki                                     Slippy on github
Blog Forever Undecided                             Twitter @koko_u
Guard Rspec + Spork による自動テスト
   サンプルとする Rails プロジェクトを作成し ます(4/6)
                            ます
   RSpec を使うためのおまじないを実行
   $ rails generate rspec:install
          create .rspec
          create spec
          create spec/spec_helper.rb




KOZAKI Tsuneaki                         Slippy on github
Blog Forever Undecided                 Twitter @koko_u
Guard Rspec + Spork による自動テスト
   サンプルとする Rails プロジェクトを作成し ます(5/6)
                            ます
   デモ用のアプリケーションをでっち上げます
   $ rails generate scaffold Article name:string title:string content:text
         invoke active_record
         create    db/migrate/20110905165645_create_articles.rb
         create    app/models/article.rb
         invoke    rspec
         create      spec/models/article_spec.rb
          route resources :articles
         invoke scaffold_controller
   ...
   RSpec でテストの雛形が作られます




KOZAKI Tsuneaki                                   Slippy on github
Blog Forever Undecided                           Twitter @koko_u
Guard Rspec + Spork による自動テスト
          サンプルとする Rails プロジェクトを作成し ます(6/6)
                                   ます
          データベースを作って
tent:text $ rake db:migrate RAILS_ENV=test
          テストが動くことを確認
          $ bundle exec rspec spec
          ........................**....
          Pending:
            Article add some examples to (or delete) /home/kozaki/minamirb/intro_guard_r
              # Not Yet Implemented
              # ./spec/models/article_spec.rb:4
            ArticlesHelper add some examples to (or delete) /home/kozaki/minamirb/intro_
              # Not Yet Implemented
              # ./spec/helpers/articles_helper_spec.rb:14
          Finished in 0.35449 seconds
          30 examples, 0 failures, 2 pending


       KOZAKI Tsuneaki                                   Slippy on github
       Blog Forever Undecided                           Twitter @koko_u
Guard Rspec + Spork による自動テスト
          Spork でテストを素早く実行できる よ う設定し ます(1/4)
                                     ます
          spork を使うためのおまじないを実行
tent:text $ bundle exec spork --bootstrap
           Using RSpec
           Bootstrapping /home/kozaki/minamirb/intro_guard_rspec/guard_demo/spec/spec_hel
           Done. Edit /home/kozaki/minamirb/intro_guard_rspec/guard_demo/spec/spec_helper


rb/intro_guard_r

/minamirb/intro_




       KOZAKI Tsuneaki                                    Slippy on github
       Blog Forever Undecided                            Twitter @koko_u
Guard Rspec + Spork による自動テスト
             Spork でテストを素早く実行できる よ う設定し ます(2/4)
                                        ます
             spec_helper.rb を編集しろ、と言われるので言われるままに編集
tent:text 01 Spork.prefork do
          02  # Loading more in this block will cause your tests to run
mo/spec/spec_helyou change any configuration or code from libraries l
          03  # if
spec/spec_helper to restart spork for it take effect.
          04  # need
          05
          06  !! ここいら辺にすべてのコードをぶちこむ !!
          07 end
          08
rb/intro_guard_r
          09 Spork.each_run do
          10  # This code will be run each time you run your specs.
          11
          12 end
/minamirb/intro_




          KOZAKI Tsuneaki                                                  Slippy on github
          Blog Forever Undecided                                          Twitter @koko_u
Guard Rspec + Spork による自動テスト
           Spork でテストを素早く実行できる よ う設定し ます(3/4)
                                      ます
          spork の稼動確認をします
tent:text 1. 別のター ミナ ル を 立ち上げ て、 spork サー バ ー を 起動します
              $ bundle exec spork
              Using RSpec
mo/spec/spec_hel
              Preloading Rails environment
spec/spec_helper
              Loading Spork.prefork block...
              Spork is ready and listening on 8989!

          2. rspec
rb/intro_guard_r でテス ト を 実行してみ ます
              $ bundle exec rspec spec --drb
/minamirb/intro_ オプション を 付け ると spork サー バ ー 側でテス ト を 実行してく れ ま
             --drb
             す
             spork サー バ ー を 起動している側のター ミナ ル に Running tests with
             ... と 出力され ていれ ば OK




        KOZAKI Tsuneaki                                  Slippy on github
        Blog Forever Undecided                          Twitter @koko_u
Guard Rspec + Spork による自動テスト
           Spork でテストを素早く実行できる よ う設定し ます(4/4)
                                      ます
          いちいちオプション --drb 付けるの面倒ですよね。
tent:text .rspec ファイルに書いておきましょう
            1 --colour
mo/spec/spec_hel
            2 --drb
spec/spec_helper
          --colour オプションは既に rails g rspec:install した時点で書かれています
          /usr/bin/time -p bundle exec rspec spec/model/article_spec.rb を実
          行してみて、時間が早くなっていることも確かめましょう
rb/intro_guard_r


/minamirb/intro_




        KOZAKI Tsuneaki                                           Slippy on github
        Blog Forever Undecided                                   Twitter @koko_u
Guard Rspec + Spork による自動テスト
          Guard を使ってテストが自動的に実行される よ うし ます(1/4)
                                       ます
          ようやく guard の設定です。でもすることは簡単
tent:text $ bundle exec guard init spork
          Writing new Guardfile to /home/kozaki/minamirb/intro_guard_rspec/guard_demo/Guardfile
mo/spec/spec_helguard added to Guardfile, feel free to edit it
          spork
spec/spec_helper
          $ bundle exec guard init rspec
          rspec guard added to Guardfile, feel free to edit it
rb/intro_guard_r spork を先に実行して下さい。
          guard init
          実行した順に設定が適用されるので、逆順で Guardfile を作成した場合は適
          当に順番を入れ替えましょう。
/minamirb/intro_




       KOZAKI Tsuneaki                                   Slippy on github
       Blog Forever Undecided                           Twitter @koko_u
Guard Rspec + Spork による自動テスト
         Guard を使ってテストが自動的に実行される よ うし ます(2/4)
                                      ます
          ひとまずこれでおっけー。動かしてみましょう
tent:text $ bundle exec guard
ec/guard_demo/Guardfilewatching at '/home/kozaki/minamirb/intro_guard_rspec/guard_demo'
          Guard is now
mo/spec/spec_hel
          Starting Spork for RSpec
spec/spec_helperRSpec
          Using
          Preloading Rails environment
          Loading Spork.prefork block...
          Spork is ready and listening on 8989!
rb/intro_guard_rserver for RSpec successfully started
          Spork
          Guard::RSpec is running, with RSpec 2!
          Running all specs
/minamirb/intro_ tests with args ["--color", "--format", "progress", "--format", "Guard"
          Running
          ...........................*....
          Pending:
            ArticlesHelper add some examples to (or delete) /home/kozaki/minamirb/intro
              # Not Yet Implemented
              # ./spec/helpers/articles_helper_spec.rb:14

          Finished in 0.47565 seconds
       KOZAKI Tsuneaki                                   Slippy on github
           32 examples,0 failures, 1 pending
       Blog Forever Undecided                           Twitter @koko_u
          Done.
Guard Rspec + Spork による自動テスト
             Guard を使ってテストが自動的に実行される よ うし ます(3/4)
                                          ます
             テストを書くと、自動的に実行されることを確認しましょう。
tent:text 01 # -*- coding: utf-8 -*-
pec/guard_demo'
ec/guard_demo/Guardfile
          02 # spec/models/article_spec.rb
mo/spec/spec_hel 'spec_helper'
          03 require
          04
spec/spec_helper
          05 describe Article do
          06  context "は name と title が必須" do
          07   subject { Article.new }
          08   it { should be_invalid }
rb/intro_guard_r should have(1).error_on(:title) }
          09   it {
          10   it { should have(1).error_on(:name) }
          11  end
-format", 12 end
/minamirb/intro_
          "Guard"
          自動的に実行されて、テスト失敗しましたか?
          RSpec の書き方はこちらでどうぞ RSpec を使い始める人が読むべき N 個の
          ドキュメント
/minamirb/intro



          KOZAKI Tsuneaki                               Slippy on github
          Blog Forever Undecided                       Twitter @koko_u
Guard Rspec + Spork による自動テスト
             Guard を使ってテストが自動的に実行される よ うし ます(4/4)
                                          ます
             モデルに適当なバリデーションを追加しましょう
tent:text 1 # app/models/article.rb
pec/guard_demo' Article < ActiveRecord::Base
ec/guard_demo/Guardfile
           2 class
mo/spec/spec_hel
           3  validates :title, :name, :presence => true
           4 end
spec/spec_helper
          やはりテストが自動的に実行され、今度はパスしましたか?
          モデルのテストしかしていないことに注意して下さい。全部のテストを実行(エン
          ターするだけ)するとコントローラ側でエラーになっていることがわかります
rb/intro_guard_r


/minamirb/intro_
-format", "Guard"


/minamirb/intro



          KOZAKI Tsuneaki                                   Slippy on github
          Blog Forever Undecided                           Twitter @koko_u
Guard Rspec + Spork による自動テスト

                                お疲れさまでし た
tent:text
pec/guard_demo'
ec/guard_demo/Guardfile
mo/spec/spec_hel
spec/spec_helper


rb/intro_guard_r


/minamirb/intro_
-format", "Guard"


/minamirb/intro



       KOZAKI Tsuneaki                       Slippy on github
       Blog Forever Undecided               Twitter @koko_u
Guard Rspec + Spork による自動テスト
          参考にし たサイト
             guard のサイ ト
             spork のサイ ト
tent:text    Curiosity Drives Me 基本このサイ ト を まる写し
pec/guard_demo'
ec/guard_demo/Guardfile
mo/spec/spec_hel
spec/spec_helper


rb/intro_guard_r


/minamirb/intro_
-format", "Guard"


/minamirb/intro



       KOZAKI Tsuneaki                              Slippy on github
       Blog Forever Undecided                      Twitter @koko_u
Guard Rspec + Spork による自動テスト
          その他便利そうな Guard-xxx
          guard-annotate
tent:text モデルのスキーマを変更すると、自動的にモデルクラスにその内容をコメントとして書いてくれる
          guard-livereload
pec/guard_demo'
ec/guard_demo/Guardfile
           ビューが変更された時に、自動的にブラウザでページをリロードしてくれる。あらかじめ、chrome な
mo/spec/spec_hel
          どにadd-onを追加しておく必要あり
spec/spec_helper
          そのほか色々あるらしい List of avairable guards

rb/intro_guard_r


/minamirb/intro_
-format", "Guard"


/minamirb/intro



       KOZAKI Tsuneaki                                Slippy on github
       Blog Forever Undecided                        Twitter @koko_u
Guard Rspec + Spork による自動テスト
          課題
          Guardfile の修正について
tent:text    結局自動的に作成され た Guardfile を 修正す ること な く ここまで来まし
             た
pec/guard_demo'
ec/guard_demo/Guardfile
mo/spec/spec_hel ァ イ ル を 増やした り、変更した い場合は 適当に修正しましょう
             監視フ
          Spork の設定
spec/spec_helper
             spork の設定も全然変更していませ ん
             実際には Spork.each_run { } に適当に設定を 追加す る必要が ありま
             しょう
rb/intro_guard_r さっ きの参考サイ ト を 見てね
             詳細は
             困っ た ら 再起動でもわりと イ け ます 。手動で再起動す るのが 面倒にな っ
             た り、不可解な エラ ー が 頻繁に出てか ら 考えても遅く は な い
/minamirb/intro_
-format", "Guard"


/minamirb/intro



       KOZAKI Tsuneaki                              Slippy on github
       Blog Forever Undecided                      Twitter @koko_u
おしま い



tent:text
pec/guard_demo'
ec/guard_demo/Guardfile
mo/spec/spec_hel
spec/spec_helper


rb/intro_guard_r


/minamirb/intro_
-format", "Guard"


/minamirb/intro

More Related Content

What's hot

今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツールaoshiman
 
勉強会 Cvml python基礎
勉強会 Cvml python基礎勉強会 Cvml python基礎
勉強会 Cvml python基礎真哉 杉野
 
composer-scriptsについて
composer-scriptsについてcomposer-scriptsについて
composer-scriptsについてHiraku Nakano
 
密着!わたしのコンソールアプリ開発環境
密着!わたしのコンソールアプリ開発環境密着!わたしのコンソールアプリ開発環境
密着!わたしのコンソールアプリ開発環境Fumihito Yokoyama
 
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12Takanori Suzuki
 
Gitのよく使うコマンド
Gitのよく使うコマンドGitのよく使うコマンド
Gitのよく使うコマンドYUKI Kaoru
 

What's hot (7)

今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
今年使ってみて良かった、Pythonモジュール、パッケージ、ツール
 
勉強会 Cvml python基礎
勉強会 Cvml python基礎勉強会 Cvml python基礎
勉強会 Cvml python基礎
 
composer-scriptsについて
composer-scriptsについてcomposer-scriptsについて
composer-scriptsについて
 
密着!わたしのコンソールアプリ開発環境
密着!わたしのコンソールアプリ開発環境密着!わたしのコンソールアプリ開発環境
密着!わたしのコンソールアプリ開発環境
 
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
「Python言語」はじめの一歩 / First step of Python / 2016 Jan 12
 
第1回python勉強会
第1回python勉強会第1回python勉強会
第1回python勉強会
 
Gitのよく使うコマンド
Gitのよく使うコマンドGitのよく使うコマンド
Gitのよく使うコマンド
 

Similar to Introduction to guard + rspec

Skinny Framework 進捗どうですか? #fud_scala
Skinny Framework 進捗どうですか? #fud_scalaSkinny Framework 進捗どうですか? #fud_scala
Skinny Framework 進捗どうですか? #fud_scalaKazuhiro Sera
 
Ruby on Rails3 Tutorial Chapter3
Ruby on Rails3 Tutorial Chapter3Ruby on Rails3 Tutorial Chapter3
Ruby on Rails3 Tutorial Chapter3Sea Mountain
 
Hyper → Highspeed → Development
Hyper → Highspeed → DevelopmentHyper → Highspeed → Development
Hyper → Highspeed → Developmentaktsk
 
Do you like scala
Do you like scalaDo you like scala
Do you like scalaYuto Suzuki
 
Scala Daysに行ってみて
Scala Daysに行ってみてScala Daysに行ってみて
Scala Daysに行ってみてKota Mizushima
 
PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門泰 増田
 
super_unkoをスーパーキレイにした
super_unkoをスーパーキレイにしたsuper_unkoをスーパーキレイにした
super_unkoをスーパーキレイにしたjiro4989
 
いまさら聞けないRake入門
いまさら聞けないRake入門いまさら聞けないRake入門
いまさら聞けないRake入門Tomoya Kawanishi
 
Scala 初めての人が Heroku で Web アプリを公開するまで
Scala 初めての人が Heroku で Web アプリを公開するまでScala 初めての人が Heroku で Web アプリを公開するまで
Scala 初めての人が Heroku で Web アプリを公開するまでHideaki Miyake
 
【LT】 怖くない恐怖のScala.js
【LT】 怖くない恐怖のScala.js【LT】 怖くない恐怖のScala.js
【LT】 怖くない恐怖のScala.jsYuto Suzuki
 
@nukokusa_botを支える技術
@nukokusa_botを支える技術@nukokusa_botを支える技術
@nukokusa_botを支える技術Joe_noh
 
capybara で快適なテスト生活を
capybara で快適なテスト生活をcapybara で快適なテスト生活を
capybara で快適なテスト生活をRyunosuke SATO
 
North Starアプリ作り,海外コミュニティ,Tips
North Starアプリ作り,海外コミュニティ,TipsNorth Starアプリ作り,海外コミュニティ,Tips
North Starアプリ作り,海外コミュニティ,TipsTasuku Takahashi
 

Similar to Introduction to guard + rspec (20)

Skinny Framework 進捗どうですか? #fud_scala
Skinny Framework 進捗どうですか? #fud_scalaSkinny Framework 進捗どうですか? #fud_scala
Skinny Framework 進捗どうですか? #fud_scala
 
Ruby on Rails3 Tutorial Chapter3
Ruby on Rails3 Tutorial Chapter3Ruby on Rails3 Tutorial Chapter3
Ruby on Rails3 Tutorial Chapter3
 
Spekではじめるユニットテスト
SpekではじめるユニットテストSpekではじめるユニットテスト
Spekではじめるユニットテスト
 
Hyper → Highspeed → Development
Hyper → Highspeed → DevelopmentHyper → Highspeed → Development
Hyper → Highspeed → Development
 
Do you like scala
Do you like scalaDo you like scala
Do you like scala
 
Scala Daysに行ってみて
Scala Daysに行ってみてScala Daysに行ってみて
Scala Daysに行ってみて
 
Haikara
HaikaraHaikara
Haikara
 
scala-kaigi1-sbt
scala-kaigi1-sbtscala-kaigi1-sbt
scala-kaigi1-sbt
 
PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門
 
実戦Scala
実戦Scala実戦Scala
実戦Scala
 
super_unkoをスーパーキレイにした
super_unkoをスーパーキレイにしたsuper_unkoをスーパーキレイにした
super_unkoをスーパーキレイにした
 
#31 愛しのst2
#31 愛しのst2#31 愛しのst2
#31 愛しのst2
 
いまさら聞けないRake入門
いまさら聞けないRake入門いまさら聞けないRake入門
いまさら聞けないRake入門
 
Scala 初めての人が Heroku で Web アプリを公開するまで
Scala 初めての人が Heroku で Web アプリを公開するまでScala 初めての人が Heroku で Web アプリを公開するまで
Scala 初めての人が Heroku で Web アプリを公開するまで
 
【LT】 怖くない恐怖のScala.js
【LT】 怖くない恐怖のScala.js【LT】 怖くない恐怖のScala.js
【LT】 怖くない恐怖のScala.js
 
@nukokusa_botを支える技術
@nukokusa_botを支える技術@nukokusa_botを支える技術
@nukokusa_botを支える技術
 
Frank-afcproxy
Frank-afcproxyFrank-afcproxy
Frank-afcproxy
 
capybara で快適なテスト生活を
capybara で快適なテスト生活をcapybara で快適なテスト生活を
capybara で快適なテスト生活を
 
North Starアプリ作り,海外コミュニティ,Tips
North Starアプリ作り,海外コミュニティ,TipsNorth Starアプリ作り,海外コミュニティ,Tips
North Starアプリ作り,海外コミュニティ,Tips
 
Code injectiontool
Code injectiontoolCode injectiontool
Code injectiontool
 

Recently uploaded

モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...博三 太田
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)UEHARA, Tetsutaro
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案sugiuralab
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)Hiroshi Tomioka
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfFumieNakayama
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineerYuki Kikuchi
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfFumieNakayama
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?akihisamiyanaga1
 

Recently uploaded (8)

モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
 

Introduction to guard + rspec

  • 1. Guard Rspec + Spork による自動テスト 小崎玄顕 平成23年10月1日(土) KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 2. Guard Rspec + Spork による自動テスト 自己紹介 名前 : 小崎 玄顕 (こざき つ ねあき ) こざき つ いっ た : @koko_u 職業 : SIer, COBOL-er, Excel-er, いろいろ … 趣味 : 読書 (読む だ け )、音楽 (聞く だ け )、プログ ラ ミン グ (パク るだ け ) 読む 、音楽 聞く 、プログ パク 興味 (soft): Ruby, Rails, Scala, Play!, ... 興味 (hard): Sony Reader, Android, ThinkPad, Happy Hacking Keyboard, ... KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 3. Guard Rspec + Spork による自動テスト 今日のお題 自動テス ト の環境と して Guard + Spork を 紹介します 実際に簡単な サン プル プロジ ェク ト を 作ります KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 4. Guard Rspec + Spork による自動テスト アンケート guard を 知っ ている? spork を 知っ ている? KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 5. Guard Rspec + Spork による自動テスト そもそもなぜテストが必要なのか? KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 6. Guard Rspec + Spork による自動テスト 実行し ないと正し いこ とを確かめられないからですね KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 7. Guard Rspec + Spork による自動テスト 例えば ... 01 # wrong_hello.rb 02 def wrong_hello(name) 03 if name.nil? 04 puts "you are nil boy" 05 elseif name.empty? 06 puts "you are empty girl" 07 else 08 puts "hello #{name}" 09 end 10 end KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 8. Guard Rspec + Spork による自動テスト 普段は何ごともなく動く 1 require './wrong_hello' 2 wrong_hello 'koko_u' 3 4 # => hello koko_u KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 9. Guard Rspec + Spork による自動テスト でも実は間違えている 1 require './wrong_hello' 2 wrong_hello '' 3 4 # => hello ...あれ? empty girl は? KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 10. Guard Rspec + Spork による自動テスト すごく間違えてる ... 1 require './wrong_hello' 2 wrong_hello nil 3 4 # => `wrong_hello': undefined method `empty?' for nil:NilClass (NoMethodError) 例外が飛んできたよ! KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 11. Guard Rspec + Spork による自動テスト ささいな typo でもその行が 実行される までエラーが発見できな でも その行が実行される い テストする し かないですね KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 12. Guard Rspec + Spork による自動テスト なぜ Guard + Spork なのか? なるべくストレスなくテストを実行したい テスト結果が notify などで表示されると、なんとなくデキる気がする 他の選択肢は autotest (Zentest) よくわからなかった watchr 新規にファイルを追加した時にうまく動かなかった KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 13. Guard Rspec + Spork による自動テスト デモ 実際に guard と spork が 動いている所を まず見せ ます 1. デモプロジェクトの直下で guard を起動する と、まずは全 てのテストが実行されます 2. その後、テストファイルもし くはソースを変更する 度に対応 する テストが実行されます KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 14. Guard Rspec + Spork による自動テスト Spork の利点 自動的にテス ト を 実行しているのは Guard の機能です た だ 、ご 存知のよう に Rails のテス ト の実行早く ありませ ん。 自動テス ト で頻繁にテス ト を 実行す る時、「 す ぐ に」 結果が 返っ てこな い と イ ラ イ ラ してしまう そこで、 Spork が あら か じ め Rails の環境ロー ド までを 済ませ ておいて、 リ ク エス ト に応じ てテス ト を 実行します どのくらい実行時間に差がある かも確認し てみまし ょう KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 15. Guard Rspec + Spork による自動テスト 能書きが長すぎた。実際にやってみまし ょう KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 16. Guard Rspec + Spork による自動テスト 前提とする 環境 OS : Ubuntu 11.04 Ruby : 1.9.2-p180 Rails : 3.1.0 Testing Framework : RSpec Mac の人は 自力で頑張っ て下さい。多分 growl に使う gem が 違う く ら い? Windows の人は ご めんな さい。 Rails 3.0.x でも動く は ず。せ っ か く リ リ ー ス され た ので 3.1.0 でやります が 、 3.1の新機能は 一切でてきませ ん。 の新機能は Testing Framework は RSpec。 Test::Unit でも gem を 差し替えるな ど 。 す れ ばできるは ずです が 、時間が な く てフ ォ ロー できませ んでした 。 KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 17. Guard Rspec + Spork による自動テスト お品書き 1. サン プル の Rails プロジ ェク ト を 作成します (6ペー ジ ) ペー 2. Spork でテス ト を 素早く 実行できるよう 設定します (4ペー ジ ) ペー 3. Guard を 使っ てテス ト が 自動的に実行され るよう 設定します (4ペー ジ ) ペー KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 18. Guard Rspec + Spork による自動テスト サンプルとする Rails プロジェクトを作成し ます(1/6) ます $ rails new guard_demo -T --skip-bundle create create README create Rakefile create config.ru create .gitignore create Gemfile create app ... Testing Framework を デ フ ォ ル ト の Test::Unit では な く RSpec に差し 替えるので、 -T オプション で test デ ィレ ク ト リ の作成を 抑止しています Rails 3.1.0 か ら 自動的に bundle install され てしまう ので、これ も抑止 しています 。 Gemfile を 修正して bundle install す るので、このタイ ミン グ です ること な いんじ ゃ ? KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 19. Guard Rspec + Spork による自動テスト サンプルとする Rails プロジェクトを作成し ます(2/6) ます Gemfile の最後に下記のコードを追加 1 group :test, :development do 2 gem 'rspec-rails' 3 gem 'spork', '~> 0.9.0.rc' 4 gem 'guard-rspec' 5 gem 'guard-spork' 6 gem 'rb-inotify' 7 gem 'libnotify' 8 end spork は 0.9.0.rc 以上でないと Rails 3.0 以降で動かないようです Mac をお使いの人は最後の 2行を下記のように変更すればいいらしい 1 group :test, :development do 2 gem 'rspec-rails' 3 gem 'spork', '~> 0.9.0.rc' 4 gem 'guard-rspec' 5 gem 'guard-spork' 6 gem 'rb-fsevent' 7 gem 'growl_notify' 8 end KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 20. Guard Rspec + Spork による自動テスト サンプルとする Rails プロジェクトを作成し ます(3/6) ます やっと bundle install します $ bundle install --path=vendor/bundle Fetching source index for http://rubygems.org/ Installing rake (0.9.2) Installing multi_json (1.0.3) Installing activesupport (3.1.0) Installing bcrypt-ruby (3.0.0) with native extensions Installing builder (3.0.0) Installing i18n (0.6.0) Installing activemodel (3.1.0) ... ここで --path オプション を 付け な いと 、最初に --skip-bundle した 意味 が な く な るよ KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 21. Guard Rspec + Spork による自動テスト サンプルとする Rails プロジェクトを作成し ます(4/6) ます RSpec を使うためのおまじないを実行 $ rails generate rspec:install create .rspec create spec create spec/spec_helper.rb KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 22. Guard Rspec + Spork による自動テスト サンプルとする Rails プロジェクトを作成し ます(5/6) ます デモ用のアプリケーションをでっち上げます $ rails generate scaffold Article name:string title:string content:text invoke active_record create db/migrate/20110905165645_create_articles.rb create app/models/article.rb invoke rspec create spec/models/article_spec.rb route resources :articles invoke scaffold_controller ... RSpec でテストの雛形が作られます KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 23. Guard Rspec + Spork による自動テスト サンプルとする Rails プロジェクトを作成し ます(6/6) ます データベースを作って tent:text $ rake db:migrate RAILS_ENV=test テストが動くことを確認 $ bundle exec rspec spec ........................**.... Pending: Article add some examples to (or delete) /home/kozaki/minamirb/intro_guard_r # Not Yet Implemented # ./spec/models/article_spec.rb:4 ArticlesHelper add some examples to (or delete) /home/kozaki/minamirb/intro_ # Not Yet Implemented # ./spec/helpers/articles_helper_spec.rb:14 Finished in 0.35449 seconds 30 examples, 0 failures, 2 pending KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 24. Guard Rspec + Spork による自動テスト Spork でテストを素早く実行できる よ う設定し ます(1/4) ます spork を使うためのおまじないを実行 tent:text $ bundle exec spork --bootstrap Using RSpec Bootstrapping /home/kozaki/minamirb/intro_guard_rspec/guard_demo/spec/spec_hel Done. Edit /home/kozaki/minamirb/intro_guard_rspec/guard_demo/spec/spec_helper rb/intro_guard_r /minamirb/intro_ KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 25. Guard Rspec + Spork による自動テスト Spork でテストを素早く実行できる よ う設定し ます(2/4) ます spec_helper.rb を編集しろ、と言われるので言われるままに編集 tent:text 01 Spork.prefork do 02 # Loading more in this block will cause your tests to run mo/spec/spec_helyou change any configuration or code from libraries l 03 # if spec/spec_helper to restart spork for it take effect. 04 # need 05 06 !! ここいら辺にすべてのコードをぶちこむ !! 07 end 08 rb/intro_guard_r 09 Spork.each_run do 10 # This code will be run each time you run your specs. 11 12 end /minamirb/intro_ KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 26. Guard Rspec + Spork による自動テスト Spork でテストを素早く実行できる よ う設定し ます(3/4) ます spork の稼動確認をします tent:text 1. 別のター ミナ ル を 立ち上げ て、 spork サー バ ー を 起動します $ bundle exec spork Using RSpec mo/spec/spec_hel Preloading Rails environment spec/spec_helper Loading Spork.prefork block... Spork is ready and listening on 8989! 2. rspec rb/intro_guard_r でテス ト を 実行してみ ます $ bundle exec rspec spec --drb /minamirb/intro_ オプション を 付け ると spork サー バ ー 側でテス ト を 実行してく れ ま --drb す spork サー バ ー を 起動している側のター ミナ ル に Running tests with ... と 出力され ていれ ば OK KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 27. Guard Rspec + Spork による自動テスト Spork でテストを素早く実行できる よ う設定し ます(4/4) ます いちいちオプション --drb 付けるの面倒ですよね。 tent:text .rspec ファイルに書いておきましょう 1 --colour mo/spec/spec_hel 2 --drb spec/spec_helper --colour オプションは既に rails g rspec:install した時点で書かれています /usr/bin/time -p bundle exec rspec spec/model/article_spec.rb を実 行してみて、時間が早くなっていることも確かめましょう rb/intro_guard_r /minamirb/intro_ KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 28. Guard Rspec + Spork による自動テスト Guard を使ってテストが自動的に実行される よ うし ます(1/4) ます ようやく guard の設定です。でもすることは簡単 tent:text $ bundle exec guard init spork Writing new Guardfile to /home/kozaki/minamirb/intro_guard_rspec/guard_demo/Guardfile mo/spec/spec_helguard added to Guardfile, feel free to edit it spork spec/spec_helper $ bundle exec guard init rspec rspec guard added to Guardfile, feel free to edit it rb/intro_guard_r spork を先に実行して下さい。 guard init 実行した順に設定が適用されるので、逆順で Guardfile を作成した場合は適 当に順番を入れ替えましょう。 /minamirb/intro_ KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 29. Guard Rspec + Spork による自動テスト Guard を使ってテストが自動的に実行される よ うし ます(2/4) ます ひとまずこれでおっけー。動かしてみましょう tent:text $ bundle exec guard ec/guard_demo/Guardfilewatching at '/home/kozaki/minamirb/intro_guard_rspec/guard_demo' Guard is now mo/spec/spec_hel Starting Spork for RSpec spec/spec_helperRSpec Using Preloading Rails environment Loading Spork.prefork block... Spork is ready and listening on 8989! rb/intro_guard_rserver for RSpec successfully started Spork Guard::RSpec is running, with RSpec 2! Running all specs /minamirb/intro_ tests with args ["--color", "--format", "progress", "--format", "Guard" Running ...........................*.... Pending: ArticlesHelper add some examples to (or delete) /home/kozaki/minamirb/intro # Not Yet Implemented # ./spec/helpers/articles_helper_spec.rb:14 Finished in 0.47565 seconds KOZAKI Tsuneaki Slippy on github 32 examples,0 failures, 1 pending Blog Forever Undecided Twitter @koko_u Done.
  • 30. Guard Rspec + Spork による自動テスト Guard を使ってテストが自動的に実行される よ うし ます(3/4) ます テストを書くと、自動的に実行されることを確認しましょう。 tent:text 01 # -*- coding: utf-8 -*- pec/guard_demo' ec/guard_demo/Guardfile 02 # spec/models/article_spec.rb mo/spec/spec_hel 'spec_helper' 03 require 04 spec/spec_helper 05 describe Article do 06 context "は name と title が必須" do 07 subject { Article.new } 08 it { should be_invalid } rb/intro_guard_r should have(1).error_on(:title) } 09 it { 10 it { should have(1).error_on(:name) } 11 end -format", 12 end /minamirb/intro_ "Guard" 自動的に実行されて、テスト失敗しましたか? RSpec の書き方はこちらでどうぞ RSpec を使い始める人が読むべき N 個の ドキュメント /minamirb/intro KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 31. Guard Rspec + Spork による自動テスト Guard を使ってテストが自動的に実行される よ うし ます(4/4) ます モデルに適当なバリデーションを追加しましょう tent:text 1 # app/models/article.rb pec/guard_demo' Article < ActiveRecord::Base ec/guard_demo/Guardfile 2 class mo/spec/spec_hel 3 validates :title, :name, :presence => true 4 end spec/spec_helper やはりテストが自動的に実行され、今度はパスしましたか? モデルのテストしかしていないことに注意して下さい。全部のテストを実行(エン ターするだけ)するとコントローラ側でエラーになっていることがわかります rb/intro_guard_r /minamirb/intro_ -format", "Guard" /minamirb/intro KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 32. Guard Rspec + Spork による自動テスト お疲れさまでし た tent:text pec/guard_demo' ec/guard_demo/Guardfile mo/spec/spec_hel spec/spec_helper rb/intro_guard_r /minamirb/intro_ -format", "Guard" /minamirb/intro KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 33. Guard Rspec + Spork による自動テスト 参考にし たサイト guard のサイ ト spork のサイ ト tent:text Curiosity Drives Me 基本このサイ ト を まる写し pec/guard_demo' ec/guard_demo/Guardfile mo/spec/spec_hel spec/spec_helper rb/intro_guard_r /minamirb/intro_ -format", "Guard" /minamirb/intro KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 34. Guard Rspec + Spork による自動テスト その他便利そうな Guard-xxx guard-annotate tent:text モデルのスキーマを変更すると、自動的にモデルクラスにその内容をコメントとして書いてくれる guard-livereload pec/guard_demo' ec/guard_demo/Guardfile ビューが変更された時に、自動的にブラウザでページをリロードしてくれる。あらかじめ、chrome な mo/spec/spec_hel どにadd-onを追加しておく必要あり spec/spec_helper そのほか色々あるらしい List of avairable guards rb/intro_guard_r /minamirb/intro_ -format", "Guard" /minamirb/intro KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u
  • 35. Guard Rspec + Spork による自動テスト 課題 Guardfile の修正について tent:text 結局自動的に作成され た Guardfile を 修正す ること な く ここまで来まし た pec/guard_demo' ec/guard_demo/Guardfile mo/spec/spec_hel ァ イ ル を 増やした り、変更した い場合は 適当に修正しましょう 監視フ Spork の設定 spec/spec_helper spork の設定も全然変更していませ ん 実際には Spork.each_run { } に適当に設定を 追加す る必要が ありま しょう rb/intro_guard_r さっ きの参考サイ ト を 見てね 詳細は 困っ た ら 再起動でもわりと イ け ます 。手動で再起動す るのが 面倒にな っ た り、不可解な エラ ー が 頻繁に出てか ら 考えても遅く は な い /minamirb/intro_ -format", "Guard" /minamirb/intro KOZAKI Tsuneaki Slippy on github Blog Forever Undecided Twitter @koko_u