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
540
How RSpec Works
penelope_zone
0
6.5k
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
84
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
podman_update_2024-12
orimanabu
1
280
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
130
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
210
ハイテク休憩
sat
PRO
2
170
日本版とグローバル版のモバイルアプリ統合の開発の裏側と今後の展望
miichan
1
130
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
260
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
170
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
190
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
250
Featured
See All Featured
KATA
mclloyd
29
14k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Typedesign – Prime Four
hannesfritz
40
2.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Code Reviewing Like a Champion
maltzj
520
39k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
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]