13. $ rspec --profile
…
!
Top 10 slowest examples (0.00226 seconds, 57.2% of total time):
Martini with a mixer #ingredients should include "vermouth"
0.00058 seconds ./spec/martini_spec.rb:15
InitializeWithAttrsSpec with attributes #pages should eq 123
0.00052 seconds ./spec/initialize_with_attrs_spec.rb:16
…
23. support/configure.rb
RSpec.configure do |c|
c.before(:suite) {} # once
c.before(:context) {} # once before each group
c.before(:example) {} # once before each example
!
c.after(:example) {} # once after each example
c.after(:context) {} # once after each group
c.after(:suite) {} # once
!
# run before each example of type :model
config.before(:example, :type => :model) {}
end
RSpec.configure
24. support/configure.rb
RSpec.configure do |c|
c.filter_run focus: true
c.run_all_when_everything_filtered = true
end
!
# in any spec file
describe "thing" do
it "does something interesting", :focus do
# ....
end
end
Inclusion
25. support/configure.rb
RSpec.configure do |c|
c.exclusion_filter = { :ruby => lambda { |version|
!(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
}}
end
!
# in any spec file
describe "something" do
it "does something", :ruby => 1.8 do
# ....
end
it "does something", :ruby => 2.1 do
# ....
Exclusion
27. martini_spec.rb
describe "Barkeep::Martini" do
before do
@martini = Barkeep::Martini.new
end
it "should have an attribute named booze" do
@martini.should respond_to(:booze)
@martini.should respond_to(:booze=)
end
it "should have an attribute named garnish" do
@martini.should respond_to(:garnish)
@martini.should respond_to(:garnish=)
end
end
Original Martini 2.X Spec
28. martini_spec.rb
describe "Barkeep::Martini" do
before do
@martini = Barkeep::Martini.new
end
it "should have an attribute named booze" do
expect(@martini).to respond_to(:booze)
expect(@martini).to respond_to(:booze=)
end
it "should have an attribute named garnish" do
expect(@martini).to respond_to(:garnish)
expect(@martini).to respond_to(:garnish=)
end
end
Martini Spec 3.0
30. martini_spec.rb
describe "Barkeep::Martini" do
include AttributeMatchers
before do
@martini = Barkeep::Martini.new
end
it "should have an attribute named booze" do
expect(@martini).to have_attribute(:booze)
end
it "should have an attribute named garnish" do
expect(@martini).to have_attribute(:garnish)
end
end
Custom Matcher
31. martini_spec.rb
describe "Barkeep::Martini" do
include AttributeMatchers
subject { Barkeep::Martini.new }
it "should have an attribute named booze" do
expect(subject).to have_attribute(:booze)
end
it "should have an attribute named garnish" do
expect(subject).to have_attribute(:garnish)
end
end
subject()
32. martini_spec.rb
describe "Barkeep::Martini" do
include AttributeMatchers
subject(:martini) { Barkeep::Martini.new }
it "should have an attribute named booze" do
expect(martini).to have_attribute(:booze)
end
it "should have an attribute named garnish" do
expect(martini).to have_attribute(:garnish)
end
end
Named subject()
33. martini_spec.rb
describe "Barkeep::Martini" do
include AttributeMatchers
subject { Barkeep::Martini.new }
it "should have an attribute named booze" do
is_expected.to have_attribute(:booze)
end
it "should have an attribute named garnish" do
is_expected.to have_attribute(:garnish)
end
end
Implicit subject()
38. martini_spec.rb
describe "Barkeep::Martini" do
before do
@martini = Barkeep::Martini.new
end
it "should have an attribute named booze" do
@martini.should respond_to(:booze)
@martini.should respond_to(:booze=)
end
it "should have an attribute named garnish" do
@martini.should respond_to(:garnish)
@martini.should respond_to(:garnish=)
end
end
Original Martini 2.X Spec
41. martini_spec.rb
describe Barkeep::Martini do
it { is_expected.to have_attribute(:booze) }
it { is_expected.to have_attribute(:glass) }
it { is_expected.to have_attribute(:garnish) }
end
Additions to Martini Spec
46. support/examples/drink.rb
shared_examples_for "a drink" do |ingredients|
it { is_expected.to have_attribute(:booze) }
it { is_expected.to have_attribute(:glass) }
!
describe "#ingredients" do
subject { super().ingredients }
it { is_expected.to be_a Array }
it { is_expected to include *ingredients }
end
end
Whiskey & Martini #ingredients
48. martini_spec.rb
describe Barkeep::Martini do
it_behaves_like "a drink"
it { is_expected.to have_attribute(:garnish) }
it { is_expected.to have_attribute(:mixer) }
context "with a mixer" do
before do
@mixer = 'vermouth'
subject.mixer = @mixer
end
it "should include mixer in ingredients" do
expect(subject.ingredients).to include @mixer
end
end
context()
49. martini_spec.rb
describe Barkeep::Martini do
it_behaves_like "a drink"
it { is_expected.to have_attribute(:garnish) }
it { is_expected.to have_attribute(:mixer) }
context "with a mixer" do
let(:mixer) { 'vermouth' }
before { subject.mixer = mixer }
!
it "should include mixer in ingredients" do
expect(subject.ingredients).to include mixer
end
end
end
let()
50. martini_spec.rb
describe Barkeep::Martini do
it_behaves_like "a drink"
it { is_expected.to have_attribute(:garnish) }
it { is_expected.to have_attribute(:mixer) }
context "with a mixer" do
let(:mixer) { 'vermouth' }
before { subject.mixer = mixer }
!
its(:ingredients) { is_expected.to
include mixer }
end
end
its()
51. martini_spec.rb
describe Barkeep::Martini do
it_behaves_like "a drink"
it { is_expected.to have_attribute(:garnish) }
it { is_expected.to have_attribute(:mixer) }
end
!
describe Barkeep::Martini, ".new with mixer" do
let(:mixer) { 'vermouth' }
subject { Barkeep::Martini.new(mixer: mixer) }
its(:ingredients) { is_expected.to include mixer }
end
its()
52. martini_spec.rb
describe Barkeep::Martini, ".new with mixer" do
let(:mixer) { 'vermouth' }
subject { Barkeep::Martini.new(:mixer => mixer) }
its(:ingredients) { is_expected.to include mixer }
its(:ingredients) { is_expected.to have(1).items }
end
have()
53. martini_spec.rb
describe Barkeep::Martini, ".new with mixer" do
let(:mixer) { 'vermouth' }
subject { Barkeep::Martini.new(:mixer => mixer) }
its(:ingredients) { is_expected.to include mixer }
it { is_expected.to have(1).ingredients }
end
have()
54. martini_spec.rb
describe Barkeep::Martini, ".dirty" do
let(:martini) { Barkeep::Martini.dirty }
subject { martini.ingredients }
!
it { is_expected.to include
'juice', 'vodka', 'olives', 'vermouth' }
!
it { is_expected.to contain_exactly
'vodka', 'vermouth', 'juice', 'olives' }
end
Array Matchers
55. martini_spec.rb
describe Barkeep::Martini, ".dirty" do
let(:martini) { Barkeep::Martini.dirty }
subject { martini.ingredients }
!
it { is_expected.to start_with 'vodka' }
it { is_expected.to end_with 'olives' }
end
Array Matchers
56. martini_spec.rb
describe Barkeep::Martini, ".dirty" do
let(:martini) { Barkeep::Martini.dirty }
subject { martini.ingredients }
!
it { is_expected.to all be_a(String) }
it { is_expected.to all start_with(/^w/) }
end
Array Matchers
57. martini_spec.rb
describe Barkeep::Martini, ".dirty" do
let(:martini) { Barkeep::Martini.dirty }
subject { martini.ingredients }
!
it { is_expected.to all
be_a(String) & start_with(/^w/) }
end
Compound Matchers
58. martini_spec.rb
describe Barkeep::Martini, ".dirty" do
let(:martini) { Barkeep::Martini.dirty }
subject { martini.ingredients }
!
it { is_expected.to match [
start_with(/^w/),
match(/olives?/)
] }
end
Composable Matchers
59. martini_spec.rb
describe Barkeep::Martini, ".dirty" do
let(:martini) { Barkeep::Martini.dirty }
subject { martini.ingredients }
!
it { is_expected.to match [
a_string_starting_with(/^w/),
a_string_matching(/olives?/)
] }
end
Composable Matchers
60. terminal_spec.rb
describe Terminal, "#printer" do
let(:epson) { stub "Espon 5000" }
let(:terminal) { Terminal.new(:ip => "1.1.1.1") }
subject { terminal.printer }
!
before do
Printer.stub(:by_ip).and_return(epson)
end
!
it { should eq epson }
end
RSpec 2.X doubles
61. terminal_spec.rb
describe Terminal, "#printer" do
let(:epson) { double "Espon 5000" }
let(:terminal) { Terminal.new(:ip => "1.1.1.1") }
subject { terminal.printer }
!
before do
allow(Printer).to receive_messages(by_ip: epson)
end
!
it { is_expected.to eq epson }
end
RSpec 3.0 doubles
62. terminal_spec.rb
describe Terminal, "#printer" do
let(:epson) { double "Espon 5000" }
let(:terminal) { Terminal.new(:ip => "1.1.1.1") }
subject { terminal.printer }
!
before do
allow(Printer).to receive_messages(by_ID: epson)
end
!
it { is_expected.to eq epson }
end
Double checking™
63. terminal_spec.rb
describe Terminal, "#printer" do
let(:epson) { stub "Espon 5000" }
let(:terminal) { Terminal.new(:ip => "1.1.1.1") }
subject { terminal.printer }
!
before do
Printer.should_receive(:by_ip)
.with("1.1.1.1").and_return(epson)
end
!
it { should eq epson }
end
RSpec 2.X message expectation
64. terminal_spec.rb
describe Terminal, "#printer" do
let(:epson) { double "Espon 5000" }
let(:terminal) { Terminal.new(:ip => "1.1.1.1") }
before do
allow(Printer).to receive_messages(by_ip: epson)
end
subject! { terminal.printer }
!
it { is_expected.to eq epson }
it "finds printer by IP" do
expect(Printer).to
have_received(:by_ip).with("1.1.1.1")
end
RSpec 3.0 message expectation