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
An RSpec 3 Talk
Search
Penelope Phippen
December 08, 2014
Technology
0
140
An RSpec 3 Talk
A talk about RSpec 3 that I gave at LRUG December 2014.
Penelope Phippen
December 08, 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
77
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
Accessibility Inspectorを活用した アプリのアクセシビリティ向上方法
hinakko
0
180
ドメイン駆動設計の実践により事業の成長スピードと保守性を両立するショッピングクーポン
lycorptech_jp
PRO
13
2.2k
東京Ruby会議12 Ruby と Rust と私 / Tokyo RubyKaigi 12 Ruby, Rust and me
eagletmt
3
870
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
240
CDKのコードレビューを楽にするパッケージcdk-mentorを作ってみた/cdk-mentor
tomoki10
0
210
FODにおけるホーム画面編成のレコメンド
watarukudo
PRO
2
280
AWSの生成AIサービス Amazon Bedrock入門!(2025年1月版)
minorun365
PRO
7
470
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
350
[IBM TechXchange Dojo]Watson Discoveryとwatsonx.aiでRAGを実現!事例のご紹介+座学②
siyuanzh09
0
110
あなたの知らないクラフトビールの世界
miura55
0
130
デジタルアイデンティティ技術 認可・ID連携・認証 応用 / 20250114-OIDF-J-EduWG-TechSWG
oidfj
2
690
re:Invent 2024のふりかえり
beli68
0
110
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Mobile First: as difficult as doing things right
swwweet
222
9k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
113
50k
Designing for Performance
lara
604
68k
Typedesign – Prime Four
hannesfritz
40
2.5k
Into the Great Unknown - MozCon
thekraken
34
1.6k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Designing Experiences People Love
moore
139
23k
Speed Design
sergeychernyshev
25
740
Site-Speed That Sticks
csswizardry
3
270
Transcript
An RSpec 3 Talk
a!/samphippen
RSpec RSpec ! ! RSpec 3
Upgrading
The easiest major gem upgrade you’ve ever done
The upgrade process
Be green and warning free on RSpec 2
gem “rspec”, “~> 2.99”
bundle install
fix warnings
gem “rspec”, “~> 3.0”
Done.
There is also Transpec
None
Verifying doubles
Who’s ever stubbed an object’s interface wrong?
class Foo def bar(beers) “#{beers} bottles of beer” end end
allow(Foo.new).to receive(:bare)
None
class Foo def bar(beers) “#{beers} bottles of beer” end end
allow(Foo.new).to receive(:bar).with(1,2)
None
Not enabled by default for upgrade installs
RSpec.configure do |config| config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true
end end
http://tinyurl.com/p6yk8ll
Verifying behaviour on pure doubles
double( :some_name_for_the_double, :call => :result )
instance_double( MyClass, :call => :result )
instance_double( MyClass, :call => :result )
instance_double( “MyClass”, :call => :result )
Let’s have some questions !!/samphippen
[email protected]
Also class_double and object_double
Composable matchers
One Expectation Per Test
expect(json[“foo”]).to eq(bar) expect(json[“baz”]).to eq(quz) expect(json[“boz”][“buz”]).to match(id_regex)
expect(json[“foo”]).to eq(bar) expect(json[“baz”]).to eq(quz) expect(json[“boz”][“buz”]).to match(id_regex)
Could write separate tests
That could be expensive
expect(json).to match( “foo” => bar, “baz” => quz, “boz” =>
{ “buz” => match(id_regex) } )
Match parts of hashes and arrays
{ “key” => { “key1” => value1, “key2” => value2,
} }
expect(foo).to match( “key” => a_hash_including( “key2”=> value2 ) )
{ “key” => [1,2,3] }
expect(foo).to match( “key” => an_array_including(1) )
Compound expressions via and and or
expect(alphabet).to start_with("a") expect(alphabet).to end_with("z")
expect(alphabet) .to start_with(“a”) .and end_with("z")
expect(stoplight.color) .to eq(“red") .or eq(“green") .or eq("yellow")
Nearly all existing RSpec matchers are composable
Works with custom matchers
Rails
Let’s have some questions !!/samphippen
[email protected]
OMG. Loading rails is the worst thing ever
files took 2.09 seconds to load
spec_helper is now split
spec_helper.rb Loads RSpec, not your app
rails_helper.rb Loads RSpec + whole rails app
Spec Types
RSpec rails used to be highly implicit
#spec/controllers/foo_controller_spec.rb describe FooController it “magically has get and assigns methods????”
get :index expect(assigns[:foo]).to be true end end
#spec/controllers/foo_controller_spec.rb describe FooController, :type => :controller it “gets the methods
from the metadata” get :index expect(assigns[:foo]).to be true end end
This applies to all Rails spec types
You can structure your specs however you like
If you badly need directory based spec inference
RSpec.configure do |config| config.infer_spec_type_from_file_location! end
http://tinyurl.com/kgllgtb
RSpec is trying to get less bad at patching methods
on to everything for you
Sytnax
The “a.should == b” syntax is not gone
It is deprecated
I strongly advise you to use expect(a).to eq(b) everywhere
a.should == b expect(a).to eq(b)
a.should_receive(:foo) expect(a).to receive(:foo)
a.stub(:foo) allow(a).to receive(:foo)
expect { }.to raise_error ! #is the same
a.any_instance.stub(:foo) allow_any_instance_of(a).to receive(:foo)
a.any_instance.should_receive(:foo) expect_any_instance_of(a).to receive(:foo)
RSpec also now has a “no monkey patching” mode
RSpec 2 monkey patches #describe
describe “” do describe “” do end it “” do
end end
RSpec.describe “” do describe “” do end it “” do
end end
bare describe is still available in RSpec contexts
This one might trip you up
be_true be_false
be_truthy be_falsy be_falsey
be true be false
Let’s have some questions a!/samphippen
[email protected]