Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
RSpec #TechLunch
Search
Livesense Inc.
PRO
April 23, 2014
Technology
63
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
RSpec #TechLunch
2012/04/04(水) @ Livesense TechLunch
発表者:塩足 拓也
Livesense Inc.
PRO
April 23, 2014
More Decks by Livesense Inc.
See All by Livesense Inc.
味覇で作るパスタが美味い - 20261001_yabaubuki.dev
livesense
PRO
0
130
Rubyはただの⾔語に⾮ず
livesense
PRO
0
580
28新卒_Webエンジニア職採用_会社説明資料
livesense
PRO
0
130
27新卒_総合職採用_会社説明資料
livesense
PRO
0
7.5k
27新卒_Webエンジニア職採用_会社説明資料
livesense
PRO
0
13k
株式会社リブセンス・転職会議 採用候補者様向け資料
livesense
PRO
0
600
株式会社リブセンス 会社説明資料(報道関係者様向け)
livesense
PRO
1
1.8k
データ基盤の負債解消のためのリプレイス
livesense
PRO
0
730
26新卒_総合職採用_会社説明資料
livesense
PRO
0
13k
Other Decks in Technology
See All in Technology
AI駆動開発の時代になったのでトヨタ生産方式から見直す
terurou
7
6k
大型犬好きによるPugの紹介
yamanoku
1
160
LambdaWebFunctionsを試してみた / exploring-lambda-web-functions
koukihosaka
1
340
DGX Spark でどこまでハッキングができるのか?
kash1064
3
1.3k
V8のArray.prototype.flatを最大約20倍速くするまでと、巨大OSSへの大規模コミットの道のり
riyaamemiya
0
290
JAWSUG TOHOKU 青森支部 「LoGoチャット」のAWS移行で 乗り越えたこと
taketakekaho
0
330
Optunaの最新リリースv5.0について
pfn
PRO
0
120
フロントエンドカンファレンス関西2026 - 速すぎるUIに、待ち時間を
teamlab
PRO
0
130
Claude の新機能を、 出た日に触って社内に届けるまで
rfdnxbro
1
170
AI コーディングと人間の認知スループット / CTO Night & Day 2026 Morning Session / AI Coding and Human Cognitive Throughput
prog893
4
1k
re:Invent初参加で変わったこと、2回目にやってみたいこと(JAWS-UG彩の国埼玉 #12)
mhrtech
2
220
コンテナ運用におけるネットワーク・コンテナコストの削減方法/How to Reduce Network and Container Costs in Container Operations
masakiokuda
1
150
Featured
See All Featured
Accessibility Awareness
sabderemane
1
250
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
WCS-LA-2024
lcolladotor
0
890
Docker and Python
trallard
47
4.2k
How to build a perfect <img>
jonoalderson
1
6.1k
A Soul's Torment
seathinner
8
3.8k
Side Projects
sachag
456
43k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
220
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
270
Transcript
RSpec Takuya Shiotari
TDDとは テストの対象となるコードを書く前にテストを書く開発手法 1. 失敗するテストコードを書く 2. テストがパスする最小限のコードを書く 3. リファクタリング 開発サイクル RED/GREEN/REFACTOR
▪ コードをクリーンな状態に保つことができる ▪ テストの実践ではない ▪ 高品質なコードをテスト担当者に引き渡すために使用する手法
BDDとは ▪ TDDの派生 ▪ 振る舞い(要求仕様)を自然言語で併記しなが らテストコードを記述 ▪ テスト≒要求仕様 ▪ BDDフレームワーク
◦ ストーリーフレームワーク(Cucumber) ◦ スペックフレームワーク(RSpec)
BDDの原則 ▪ 十分といったら十分 ◦ 必要最低限のことを行うべき ◦ それ以上の労力は無駄 ▪ ステークホルダーに価値をもたらす ◦
目に見える価値をもたらすためにあるべき ▪ すべては振る舞いから ◦ アプリケーションの振る舞いをステークホルダーの視点 に立って説明できるのと同様に、低レベルのコードの振 る舞いを、それを利用する他のコードという視点から説 明する
RSpec導入理由 ▪ プログラムの変更によってデグレードしてないか不安 ▪ コードのサイズが大きくなるとリファクタリングが難しい ▪ ドキュメントをBTSやツールで管理しても他のドキュメントや ソフトウェアと一致しなくなる ▪ 過度な設計をしたくない
⇒テストコードと要求仕様を併記するので常に一致 ⇒自動化したリグレッションテスト ⇒カバレッジが100%のテストがあれば安心してリファクタリング できる ⇒具象的なコードから始まり抽象化するため、適切な抽象化 で抑えられる
RSpec ▪ 2005年にSteven Bakerによって開発 ▪ 振舞を記述するためのDSLを提供するフレームワーク ▪ RSpecは全てがユニットテスト TDDの場合 RSpecの場合
アサーション (コードの検証) エクスペくテーション (期待すること) テスト サンプルコード テストケース サンプルグループ
RSpecのインストール $ gem install rspec ▪ 単独で利用する場合 ▪ railsで利用する場合 group
:develop, :test do gem 'rspec-rails' end RAILS_ROOT/Gemfile
サンプルスペックファイル describe Stack do before do @stack = Stack.new @stack.push
:item end describe "#peek" do it "should return the top element" do @stack.peek.should == :item end it "should not remove the top element" do @stack.peek @stack.size.should == 1 end end end stack_spec.rb フック サンプルコード サンプルコード サンプル グループ
スペックファイルの構成
RSpecの実行 フィルタ サンプルコード サンプルコード サンプル グループ $ rspec stack_spec.rb -c
.. Finished in 0.00063 seconds 2 examples, 0 failures ▪ 単独で利用する場合 ▪ railsで利用する場合 $ bundle exec rspec spec/views/home/index.html.erb_spec.rb .................... Finished in 10.79 seconds 20 examples, 0 failures
specdoc形式のフォーマット $ rspec stack_spec.rb -c -fd Stack #peek should return
the top element should not remove the top element Finished in 0.00072 seconds 2 examples, 0 failures
考察 ▪ 考え方になれるまで大変 ▪ Mockの使い方が鍵 ▪ RSpec単体では遅い ⇒ sporkを使って高速化 ▪
RSpec on Railsでは全てを単体テストにできる ので関心が自然に分離される ▪ eureeekaは英語で記述したが、日本語にしても よかったかも…
次回
RSpec gemの構成 ▪ rspec-core ◦ describeやitなどの実行可能なサンプルを記述できる構 造を提供 ▪ rspec-expectations ◦
サンプル内でオブジェクトの期待する結果を表現 ▪ rspec-mocks ◦ rspec-mocksはテストダブルフレームワーク ◦ テストダブル、メソッドスタブ、メッセージエクスペくテー ションをサポート
RSpec::Core ▪ describe ◦ サンプルグループを宣言 ▪ it ◦ サンプルを宣言 ▪
pending ◦ サンプルを保留扱いとして宣言 ▪ before、after ◦ サンプルの実行前後をフック
RSpec::Expectations ▪ should(matcher)とshould_not(matcher) ◦ matcher.matches?(self)がtrueの場合:次の行へ ◦ matcher.matches?(self)がfalseの場合:例外発生 ▪ RSpecに組み込まれているマッチャ ◦
equal、include、respond_to, raise_error ▪ 述語マッチャ ◦ be_empty、be_instance_of ▪ 演算子を使った式 ◦ result.should.==(3) → result.should == 3 ◦ result.should.(be.<(7)) → result.should be < 7
RSpec::Mocks ▪ テストダブル ◦ サンプルにおいて別のオブジェクトの代役を務めるオブジェクト ▪ メソッドスタブ ◦ サンプルコードの実行中にあらかじめ定義されたレスポンスを返すようにプ ログラムできるメソッド
▪ メッセージエクスペくテーション ◦ 一度も呼び出されないとエラーになるメソッドスタブ ▪ 役割に焦点を合わせる ▪ やり取りに焦点を合わせる ▪ インターフェースの発見 ▪ 依存関係を実装せずに作業をすすめる ▪ 依存関係からの分離 ▪ 不確定性からの分離