Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Anatomy of a mocked call - RuLu 2014
Search
Penelope Phippen
June 18, 2014
Technology
0
160
The Anatomy of a mocked call - RuLu 2014
Penelope Phippen
June 18, 2014
Tweet
Share
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
530
How RSpec Works
penelope_zone
0
6.4k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
75
Teaching RSpec to play nice with Rails
penelope_zone
2
120
Little machines that eat strings
penelope_zone
1
80
What is processor (brighton ruby edition)
penelope_zone
0
94
What is processor?
penelope_zone
1
340
extremely defensive coding - rubyconf edition
penelope_zone
0
250
Agile, etc.
penelope_zone
2
210
Other Decks in Technology
See All in Technology
顧客が本当に必要だったもの - パフォーマンス改善編 / Make what is needed
soudai
24
6.8k
ネット広告に未来はあるか?「3rd Party Cookie廃止とPrivacy Sandboxの効果検証の裏側」 / third-party-cookie-privacy
cyberagentdevelopers
PRO
1
130
CAMERA-Suite: 広告文生成のための評価スイート / ai-camera-suite
cyberagentdevelopers
PRO
3
270
君は隠しイベントを見つけれるか?
mujyun
0
290
【若手エンジニア応援LT会】AWSで繋がり、共に成長! ~コミュニティ活動と新人教育への挑戦~
kazushi_ohata
0
180
カメラを用いた店内計測におけるオプトインの仕組みの実現 / ai-optin-camera
cyberagentdevelopers
PRO
1
120
急成長中のWINTICKETにおける品質と開発スピードと向き合ったQA戦略と今後の展望 / winticket-autify
cyberagentdevelopers
PRO
1
160
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
マネジメント視点でのre:Invent参加 ~もしCEOがre:Inventに行ったら~
kojiasai
0
460
CyberAgent 生成AI Deep Dive with Amazon Web Services / genai-aws
cyberagentdevelopers
PRO
1
480
APIテスト自動化の勘所
yokawasa
7
4.1k
コンテンツを支える 若手ゲームクリエイターの アートディレクションの事例紹介 / cagamefi-game
cyberagentdevelopers
PRO
1
130
Featured
See All Featured
Writing Fast Ruby
sferik
626
61k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
14
1.9k
How GitHub (no longer) Works
holman
311
140k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Designing the Hi-DPI Web
ddemaree
280
34k
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Optimizing for Happiness
mojombo
376
69k
Practical Orchestrator
shlominoach
186
10k
Transcript
The Anatomy of a mocked call
!/samphippen
Hello everyone :)
Why is testing?
x`
None
Tests force behaviour
Without tests Anything could happen
Unit Testing makes me happy :)
Stubbing and mocking
Stubbing
Fake response to a method
allow(x).to( receive(:foo).and_return(3) )
x.foo will return 3
Mocking
Testing methods get called
expect(x).to receive(:foo)
Test fails if x does not receive foo
expect(x).to receive(:foo).with(3)
expect(x).to( receive(:foo) .with(3) .exactly(4).times )
How does it work?
Stubs
it “does something” do allow(foo).to receive(:bar) expect(foo.bar).to eq(nil) end
it “does something” do allow(foo).to receive(:bar) expect(foo.bar).to eq(nil) end
allow is syntax sugar for AllowanceTarget.new
Allowance Target
Allowance Target TargetBase
AllowanceTarget calls #delegate_to with :setup_allowance on TargetBase
Allowance Target TargetBase
TargetBase defines #to on AllowanceTarget
Defined #to calls :setup_allowance on matchers passed to #to
Allowance Target TargetBase
it “does something” do allow(foo).to receive(:bar) expect(foo.bar).to eq(nil) end
Things passed to #to are called “matchers”
Receive is a commonly used mocking/stubbing matcher
AllowanceTarget#to calls setup_allowance on the passed matcher
Allowance Target TargetBase Allowance Target TargetBase Receive
Receive#setup_allowance creates a Mock Proxy for object
Receive#setup_allowance calls #add_stub on the Mock Proxy
Allowance Target TargetBase Receive Proxy
Proxy objects manage all mocks/stubs for an object
it “does something” do allow(foo).to receive(:bar) allow(foo).to receive(:baz) expect(foo.bar).to eq(nil)
end
Proxy#add_stub creates a MethodDouble for the stubbed method
Allowance Target TargetBase Proxy Receive Method Double
MethodDouble saves the original implementation
MethodDouble creates stub implementation
it “does something” do allow(foo).to receive(:bar) expect(foo.bar).to eq(nil) end
it “does something” do allow(foo).to receive(:bar) expect(foo.bar).to eq(nil) end
foo.bar is the stubbed implementation
Call foo.bar
Method Double Call foo.bar
Method Double Call foo.bar Proxy
Method Double Call foo.bar Proxy Invokes stub
Method Double Call foo.bar Proxy Invokes stub Return value
it “does something” do allow(foo).to receive(:bar) expect(foo.bar).to eq(nil) end
RSpec manages lifecycle of mocks
RSpec::Mocks.setup
RSpec::Mocks.setup Your test
RSpec::Mocks.setup Your test RSpec::Mocks.verify
RSpec::Mocks.teardown RSpec::Mocks.setup Your test RSpec::Mocks.verify
All of this happens in a mock space
Allowance Target TargetBase Proxy Receive Method Double
RSpec::Mocks.teardown calls #reset_all on the current Space
Space#reset_all calls #reset on each proxy
Proxy#reset calls #reset on each MethodDouble
MethodDouble#reset returns original methods to objects
RSpec::Mocks.teardown RSpec::Mocks.setup Your test RSpec::Mocks.verify
Phew
Mocks
it “does something” do expect(foo).to receive(:bar).with(3) expect(foo.bar(3)).to eq(nil) end
expect is syntax sugar for ExpectationTarget.new
Expectation Target TargetBase Proxy Receive Method Double
Calls #setup_expectation
it “does something” do expect(foo).to receive(:bar).with(3) expect(foo.bar(3)).to eq(nil) end
Receive stores details of stubbed/ mocked implementation
Expectation Target TargetBase Proxy Receive Method Double
Method Double Call foo.bar Proxy Invokes stub Return value
Proxy callback checks arguments
Proxy raises if args don’t match
it “does something” do expect(foo).to receive(:bar).with(3) expect(foo.bar(3)).to eq(nil) end
RSpec::Mocks.teardown RSpec::Mocks.setup Your test RSpec::Mocks.verify
RSpec::Mocks.verify calls Space#verify_all
Space#verify_all calls #verify on each Proxy
Proxy raises if mock not called
And that’s how it all works :)
RSpec::Mocks.teardown RSpec::Mocks.setup Your test RSpec::Mocks.verify
expect(x).to receive(:foo) allow(x).to receive(:foo)
Target TargetBase
expect(x).to receive(:foo) allow(x).to receive(:foo)
Target TargetBase
expect(x).to receive(:foo) allow(x).to receive(:foo)
Receive
Proxy Receive
Proxy Receive Method Double
Target TargetBase Proxy Receive Method Double
Method Double Call foo.bar Proxy Invokes stub Return value
Target TargetBase Proxy Receive Method Double
RSpec 3.0.0 is out Please use it
tinyurl.com/ samrl2014
Let’s have some questions !/samphippen
[email protected]