SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
EveryDayRailsRSpec
リューノス読書会
2015-10-26
第六章
高度なコントローラスペック
@__akai
16歳JK(大嘘)
コントローラの基礎は
マスターしましたか?
……あたぼうよ!
第六章 概要
●
ロールを分けてテストする
– ロール…… 管理者や一般ユーザー、ゲストなど、アカウント
の切り分け
ロールのテストをするときに考えること
●
認証の処理のシミュレート
– 閲覧者(ユーザ)はログインしているのかどうか
– ユーザの持つロールはなにか
※ 本書のアプリでは、ユーザとしてログインしなければ、連絡先の追加や編
集はできず、管理者ユーザでなければ他のユーザの追加ができない
アイツとはヨかったケド、
アイツはダメ……
ユーザ作成ファクトリ
1 FactoryGirl.define do
2 factory :user do
3 email { Faker::Internet.email }
4 password 'secret'
5 password_confirmation 'secret'
6 factory :admin do
7 admin true
8 end
9 end
10 end
spec/factories/users.rb
ユーザ作成ファクトリ
1 FactoryGirl.define do
2 factory :user do
3 email { Faker::Internet.email }
4 password 'secret'
5 password_confirmation 'secret'
6 factory :admin do
7 admin true
8 end
9 end
10 end
●
create(:user)でユーザが作成で
きるようになる
– 子ファクトリにadminフラグ
を追加してあるので、管理者
ロールを付加したユーザも作
成可
spec/factories/users.rb
かんたんに
たくさん
ふえるね!
factory! factory!
factory!
factory!
管理者がアクセスした場合のテスト
1 describe "administrator access" do
2 before :each do
3 user = create(:admin)
4 session[:user_id] = user.id
5 end
6 describe 'GET #index' do
7 # params[:letter] がある場合
8 context 'with params[:letter]' do
9 # 指定された文字で始まる連絡先を配列に集めること
10 it "populates an array of contacts starting with the letter" do
11 smith = create(:contact, lastname: 'Smith')
12 jones = create(:contact, lastname: 'Jones')
13 get :index, letter: 'S'
14 expect(assigns(:contacts)).to match_array([smith])
15 end
16 # :index テンプレートを表示すること
17 it "renders the :index template" do
18 get :index, letter: 'S'
19 expect(response).to render_template :index
20 end
21 end
22 # params[:letter] がない場合
23 context 'without params[:letter]' do
24 # すべての連絡先を配列に集めること
25 it "populates an array of all contacts" do
26 smith = create(:contact, lastname: 'Smith')
27 jones = create(:contact, lastname: 'Jones')
28 get :index
29 expect(assigns(:contacts)).to match_array([smith, jones])
30 end
30 end
31 # :index テンプレートを表示すること
32 it "renders the :index template" do
33 get :index
34 expect(response).to render_template :index
35 end
36 end
37 end
38 describe 'GET #show' do
39 # @contact に要求された連絡先を割り当てること
40 it "assigns the requested contact to @contact" do
41 contact = create(:contact)
42 get :show, id: contact
43 expect(assigns(:contact)).to eq contact
44 end
45 # :show テンプレートを表示すること
46 it "renders the :show template" do
47 contact = create(:contact)
48 get :show, id: contact
49 expect(response).to render_template :show
50 end
51 end
52 # などなど ...
53 end
spec/controllers/contacts_controller_spec.rb
管理者がアクセスした場合のテスト
1 describe "administrator access" do
2 before :each do
3 user = create(:admin)
4 session[:user_id] = user.id
5 end
6 describe 'GET #index' do
7 # params[:letter] がある場合
8 context 'with params[:letter]' do
9 # 指定された文字で始まる連絡先を配列に集めること
10 it "populates an array of contacts starting with the letter" do
11 smith = create(:contact, lastname: 'Smith')
12 jones = create(:contact, lastname: 'Jones')
13 get :index, letter: 'S'
14 expect(assigns(:contacts)).to match_array([smith])
15 end
16 # :index テンプレートを表示すること
17 it "renders the :index template" do
18 get :index, letter: 'S'
19 expect(response).to render_template :index
20 end
21 end
22 # params[:letter] がない場合
23 context 'without params[:letter]' do
24 # すべての連絡先を配列に集めること
25 it "populates an array of all contacts" do
26 smith = create(:contact, lastname: 'Smith')
27 jones = create(:contact, lastname: 'Jones')
28 get :index
29 expect(assigns(:contacts)).to match_array([smith, jones])
30 end
30 end
31 # :index テンプレートを表示すること
32 it "renders the :index template" do
33 get :index
34 expect(response).to render_template :index
35 end
36 end
37 end
38 describe 'GET #show' do
39 # @contact に要求された連絡先を割り当てること
40 it "assigns the requested contact to @contact" do
41 contact = create(:contact)
42 get :show, id: contact
43 expect(assigns(:contact)).to eq contact
44 end
45 # :show テンプレートを表示すること
46 it "renders the :show template" do
47 contact = create(:contact)
48 get :show, id: contact
49 expect(response).to render_template :show
50 end
51 end
52 # などなど ...
53 end
spec/controllers/contacts_controller_spec.rb
なにをしたの?
●
既存のテスト項目を
describe "administrator
access" do~end
で囲み、直後にbeforeブロック
を追加
– テスト実行前に管理者アカウ
ントを作成し、ログインをシ
ミュレーション
1 describe "administrator access" do
2 before :each do
3 user = create(:admin)
4 session[:user_id] = user.id
5 end
spec/controllers/contacts_controller_spec.rb
一般ユーザのテスト
●
管理者ロールとほぼ同じ 1 describe "user access" do
2 before :each do
3 user = create(:user)
4 session[:user_id] = user.id
5 end
spec/controllers/contacts_controller_spec.rb
ゲストユーザのテスト
1 describe "guest access" do
2 # GET #index と GET #show の example は管理者と
3 # ユーザーの example と同じ
4 describe 'GET #new' do
5 # ログインを要求すること
6 it "requires login" do
7 get :new
8 expect(response).to redirect_to login_url
9 end
10 end
11 describe 'GET #edit' do
12 # ログインを要求すること
13 it "requires login" do
14 contact = create(:contact)
15 get :edit, id: contact
16 expect(response).to redirect_to login_url
17 end
18 end
19 describe "POST #create" do
20 # ログインを要求すること
21 it "requires login" do
22 post :create, id: create(:contact),
23 contact: attributes_for(:contact)
24 expect(response).to redirect_to login_url
25 end
26 end
27 describe 'PATCH #update' do
28 # ログインを要求すること
29 it "requires login" do
30 put :update, id: create(:contact),
31 contact: attributes_for(:contact)
32 expect(response).to redirect_to login_url
33 end
34 end
35 describe 'DELETE #destroy' do
36 # ログインを要求すること
37 it "requires login" do
38 delete :destroy, id: create(:contact)
39 expect(response).to redirect_to login_url
40 end
41 end
42 end spec/controllers/contacts_controller_spec.rb
ゲストユーザの仕様
●
ゲストはコントローラの処理を実行できない
– 代わりにlogin_urlへリダイレクトされる
●
コントローラのbefore_actionで実装済
その他のロールによる認可
1 describe 'user access' do
2 before :each do
3 @user = create(:user)
4 session[:user_id] = @user.id
5 end
6 describe 'GET #index' do
7 # ユーザーを @users に集めること
8 it "collects users into @users" do
9 user = create(:user)
10 get :index
11 expect(assigns(:users)).to match_array [@user,user]
12 end
13 # :index テンプレートを表示すること
14 it "renders the :index template" do
15 get :index
16 expect(response).to render_template :index
17 end
18 end
19 # GET #new はアクセスを拒否すること
20 it "GET #new denies access" do
21 get :new
22 expect(response).to redirect_to root_url
23 end
24 # POST #create はアクセスを拒否すること
25 it "POST #create denies access" do
26 post :create, user: attributes_for(:user)
27 expect(response).to redirect_to root_url
28 end
29 end
spec/controllers/users_controller_spec.rb
ロール Index Show Create Update Destroy
管理者 ○ ○ ○ ○ ○
編集者 ○ ○ ○ ○ ○
作者 ○ ○ ○ ○ ×
メンバー ○ ○ × × ×
ゲスト ○ × × × ×
ロールの実装やテストの際は、こういう表を作ると便利!
thanx!
●
sexy muscle | Flickr - Photo Sharing!
https://www.flickr.com/photos/babagozum/3928521527/
●
Thinking | Flickr - Photo Sharing!
https://www.flickr.com/photos/aigle_dore/7914619060/
●
Spring babies are out! (84/365) | Flickr - Photo Sharing!
https://www.flickr.com/photos/chungholeung/8719051968/

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsWSO2
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptxsn679259
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。iPride Co., Ltd.
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。iPride Co., Ltd.
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...Toru Tamaki
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video UnderstandingToru Tamaki
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Gamesatsushi061452
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。iPride Co., Ltd.
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルCRI Japan, Inc.
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイスCRI Japan, Inc.
 

Kürzlich hochgeladen (10)

Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 

Empfohlen

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Empfohlen (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

RSpecによるRailsテスト入門 第六章 高度なコントローラスペック

  • 5. 第六章 概要 ● ロールを分けてテストする – ロール…… 管理者や一般ユーザー、ゲストなど、アカウント の切り分け
  • 6. ロールのテストをするときに考えること ● 認証の処理のシミュレート – 閲覧者(ユーザ)はログインしているのかどうか – ユーザの持つロールはなにか ※ 本書のアプリでは、ユーザとしてログインしなければ、連絡先の追加や編 集はできず、管理者ユーザでなければ他のユーザの追加ができない
  • 8. ユーザ作成ファクトリ 1 FactoryGirl.define do 2 factory :user do 3 email { Faker::Internet.email } 4 password 'secret' 5 password_confirmation 'secret' 6 factory :admin do 7 admin true 8 end 9 end 10 end spec/factories/users.rb
  • 9. ユーザ作成ファクトリ 1 FactoryGirl.define do 2 factory :user do 3 email { Faker::Internet.email } 4 password 'secret' 5 password_confirmation 'secret' 6 factory :admin do 7 admin true 8 end 9 end 10 end ● create(:user)でユーザが作成で きるようになる – 子ファクトリにadminフラグ を追加してあるので、管理者 ロールを付加したユーザも作 成可 spec/factories/users.rb
  • 11. 管理者がアクセスした場合のテスト 1 describe "administrator access" do 2 before :each do 3 user = create(:admin) 4 session[:user_id] = user.id 5 end 6 describe 'GET #index' do 7 # params[:letter] がある場合 8 context 'with params[:letter]' do 9 # 指定された文字で始まる連絡先を配列に集めること 10 it "populates an array of contacts starting with the letter" do 11 smith = create(:contact, lastname: 'Smith') 12 jones = create(:contact, lastname: 'Jones') 13 get :index, letter: 'S' 14 expect(assigns(:contacts)).to match_array([smith]) 15 end 16 # :index テンプレートを表示すること 17 it "renders the :index template" do 18 get :index, letter: 'S' 19 expect(response).to render_template :index 20 end 21 end 22 # params[:letter] がない場合 23 context 'without params[:letter]' do 24 # すべての連絡先を配列に集めること 25 it "populates an array of all contacts" do 26 smith = create(:contact, lastname: 'Smith') 27 jones = create(:contact, lastname: 'Jones') 28 get :index 29 expect(assigns(:contacts)).to match_array([smith, jones]) 30 end 30 end 31 # :index テンプレートを表示すること 32 it "renders the :index template" do 33 get :index 34 expect(response).to render_template :index 35 end 36 end 37 end 38 describe 'GET #show' do 39 # @contact に要求された連絡先を割り当てること 40 it "assigns the requested contact to @contact" do 41 contact = create(:contact) 42 get :show, id: contact 43 expect(assigns(:contact)).to eq contact 44 end 45 # :show テンプレートを表示すること 46 it "renders the :show template" do 47 contact = create(:contact) 48 get :show, id: contact 49 expect(response).to render_template :show 50 end 51 end 52 # などなど ... 53 end spec/controllers/contacts_controller_spec.rb
  • 12. 管理者がアクセスした場合のテスト 1 describe "administrator access" do 2 before :each do 3 user = create(:admin) 4 session[:user_id] = user.id 5 end 6 describe 'GET #index' do 7 # params[:letter] がある場合 8 context 'with params[:letter]' do 9 # 指定された文字で始まる連絡先を配列に集めること 10 it "populates an array of contacts starting with the letter" do 11 smith = create(:contact, lastname: 'Smith') 12 jones = create(:contact, lastname: 'Jones') 13 get :index, letter: 'S' 14 expect(assigns(:contacts)).to match_array([smith]) 15 end 16 # :index テンプレートを表示すること 17 it "renders the :index template" do 18 get :index, letter: 'S' 19 expect(response).to render_template :index 20 end 21 end 22 # params[:letter] がない場合 23 context 'without params[:letter]' do 24 # すべての連絡先を配列に集めること 25 it "populates an array of all contacts" do 26 smith = create(:contact, lastname: 'Smith') 27 jones = create(:contact, lastname: 'Jones') 28 get :index 29 expect(assigns(:contacts)).to match_array([smith, jones]) 30 end 30 end 31 # :index テンプレートを表示すること 32 it "renders the :index template" do 33 get :index 34 expect(response).to render_template :index 35 end 36 end 37 end 38 describe 'GET #show' do 39 # @contact に要求された連絡先を割り当てること 40 it "assigns the requested contact to @contact" do 41 contact = create(:contact) 42 get :show, id: contact 43 expect(assigns(:contact)).to eq contact 44 end 45 # :show テンプレートを表示すること 46 it "renders the :show template" do 47 contact = create(:contact) 48 get :show, id: contact 49 expect(response).to render_template :show 50 end 51 end 52 # などなど ... 53 end spec/controllers/contacts_controller_spec.rb
  • 13. なにをしたの? ● 既存のテスト項目を describe "administrator access" do~end で囲み、直後にbeforeブロック を追加 – テスト実行前に管理者アカウ ントを作成し、ログインをシ ミュレーション 1 describe "administrator access" do 2 before :each do 3 user = create(:admin) 4 session[:user_id] = user.id 5 end spec/controllers/contacts_controller_spec.rb
  • 14. 一般ユーザのテスト ● 管理者ロールとほぼ同じ 1 describe "user access" do 2 before :each do 3 user = create(:user) 4 session[:user_id] = user.id 5 end spec/controllers/contacts_controller_spec.rb
  • 15. ゲストユーザのテスト 1 describe "guest access" do 2 # GET #index と GET #show の example は管理者と 3 # ユーザーの example と同じ 4 describe 'GET #new' do 5 # ログインを要求すること 6 it "requires login" do 7 get :new 8 expect(response).to redirect_to login_url 9 end 10 end 11 describe 'GET #edit' do 12 # ログインを要求すること 13 it "requires login" do 14 contact = create(:contact) 15 get :edit, id: contact 16 expect(response).to redirect_to login_url 17 end 18 end 19 describe "POST #create" do 20 # ログインを要求すること 21 it "requires login" do 22 post :create, id: create(:contact), 23 contact: attributes_for(:contact) 24 expect(response).to redirect_to login_url 25 end 26 end 27 describe 'PATCH #update' do 28 # ログインを要求すること 29 it "requires login" do 30 put :update, id: create(:contact), 31 contact: attributes_for(:contact) 32 expect(response).to redirect_to login_url 33 end 34 end 35 describe 'DELETE #destroy' do 36 # ログインを要求すること 37 it "requires login" do 38 delete :destroy, id: create(:contact) 39 expect(response).to redirect_to login_url 40 end 41 end 42 end spec/controllers/contacts_controller_spec.rb
  • 17. その他のロールによる認可 1 describe 'user access' do 2 before :each do 3 @user = create(:user) 4 session[:user_id] = @user.id 5 end 6 describe 'GET #index' do 7 # ユーザーを @users に集めること 8 it "collects users into @users" do 9 user = create(:user) 10 get :index 11 expect(assigns(:users)).to match_array [@user,user] 12 end 13 # :index テンプレートを表示すること 14 it "renders the :index template" do 15 get :index 16 expect(response).to render_template :index 17 end 18 end 19 # GET #new はアクセスを拒否すること 20 it "GET #new denies access" do 21 get :new 22 expect(response).to redirect_to root_url 23 end 24 # POST #create はアクセスを拒否すること 25 it "POST #create denies access" do 26 post :create, user: attributes_for(:user) 27 expect(response).to redirect_to root_url 28 end 29 end spec/controllers/users_controller_spec.rb
  • 18. ロール Index Show Create Update Destroy 管理者 ○ ○ ○ ○ ○ 編集者 ○ ○ ○ ○ ○ 作者 ○ ○ ○ ○ × メンバー ○ ○ × × × ゲスト ○ × × × × ロールの実装やテストの際は、こういう表を作ると便利!
  • 19. thanx! ● sexy muscle | Flickr - Photo Sharing! https://www.flickr.com/photos/babagozum/3928521527/ ● Thinking | Flickr - Photo Sharing! https://www.flickr.com/photos/aigle_dore/7914619060/ ● Spring babies are out! (84/365) | Flickr - Photo Sharing! https://www.flickr.com/photos/chungholeung/8719051968/