Share
Embed
RSpec Shared Examples
Introduction to RSpec Shared Examples
Links on the Resources Slide:
RSpec Shared Examples - https://relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples
Stephanie Viccari
December 15, 2020
Transcript
25 slides
-
W at re ha ed xa pl s? S e
s r e c r e s e s. T r o e t : 4 D t 4 E i c 2 -
F at re eq es #1 class RevenueByLocationReport def description
"Forecasted billing amounts for each location" end def headers ["Location", "Revenue Forecast"] end end 3 -
F at re eq es #1 - T st RSpec.describe
RevenueByLocationReport do describe "description" do it "returns a description of the reports purpose" do report = RevenueByLocationReport.new description = report.description expect(description).to eq("Forecasted billing amounts for each location") end end describe "headers" do it "returns descriptive headers" do report = RevenueByLocationReport.new headers = report.headers expect(headers).to eq(["Location", "Revenue Forecast"]) end end end 4 -
F at re eq es #2 class RevenueByProjectReport def description
"Forecasted billing amounts for each project" end def headers ["Project", "Location", "Revenue Forecast"] end end 5 -
F at re eq es #2 - T st RSpec.describe
RevenueByProjectReport do describe "description" do it "returns a description of the reports purpose" do report = RevenueByProjectReport.new description = report.description expect(description).to eq("Forecasted billing amounts for each project") end end describe "headers" do it "returns descriptive headers" do report = RevenueByProjectReport.new headers = report.headers expect(headers).to eq(["Project", "Location", "Revenue Forecast"]) end end end 6 -
S ar d eh vi r RSpec.describe RevenueByLocationReport do describe
"description" do it "returns a description of the reports purpose" do # verify the description end end describe "headers" do it "returns descriptive headers" do # verify the headers end end end RSpec.describe RevenueByProjectReport do describe "description" do it "returns a description of the reports purpose" do # verify the description end end describe "headers" do it "returns descriptive headers" do # verify the headers end end end 7 -
D n ng S ar d xa pl # spec/shared_examples/report_examples.rb
RSpec.shared_examples "a report" do describe "description" do it "returns a String describing the reports purpose" do report = described_class.new expect(report.description).to be_a String end end describe "headers" do it "returns an Array of descriptive headers" do report = described_class.new expect(report.headers).to be_an Array expect(report.headers).to be_present end end end 8 -
d sc ib d_c as RSpec.describe User do describe "described_class"
do it "references the class being described" do puts described_class # => User puts described_class.new # => #<User:0x00007fe605872510> expect(described_class).to eq(User) # => true end end end 9 -
d sc ib d_c as - G ve a tr
ng RSpec.describe "User" do describe "described_class" do it "references the described class" do puts described_class # => nil described_class.new # => undefined method on nil end end end 1 -
I pl ci S bj ct RSpec.describe Array do describe
"implicit subject" do context "given a class" do it "sets the subject to an instance of the class" do puts subject.inspect #=> [] expect(subject).to be_empty end end end end 1 -
E pl ci S bj ct RSpec.describe Array do subject
{ [1,2,3] } describe "explicit subject" do it "sets the subject to an instance of the class" do expect(subject).to eq([1,2,3]) end end end 1 -
R pe S bj ct s et S 4 m
d h 4 S R (o h s a ) 4 L -e e 4 U o o -l s * 4 U o c m s *E o -l s : it { is_expected.to be true } L 4 m d h 4 S h s a 4 L -e e 4 u e r l 4 u c s a r b c e 1 -
R po t ha ed xa pl s # spec/shared_examples/report_examples.rb
RSpec.shared_examples "a report" do describe "description" do it "returns a String describing the reports purpose" do report = described_class.new expect(report.description).to be_a String end end describe "headers" do it "returns an Array of descriptive headers" do report = described_class.new expect(report.headers).to be_an Array expect(report.headers).to be_present end end end 1 -
D n ng S ar d xa pl - R
fa to # spec/shared_examples/report_examples.rb RSpec.shared_examples "a report" do let(:report) { described_class.new } describe "description" do it "returns a String describing the reports purpose" do expect(report.description).to be_a String end end describe "headers" do it "returns an Array of descriptive headers" do expect(report.headers).to be_an Array expect(report.headers).to be_present end end end 1 -
R vi it he es s RSpec.describe RevenueByLocationReport do describe
"description" do it "returns a description of the reports purpose" do # verify the description end end describe "headers" do it "returns descriptive headers" do # verify the headers end end end RSpec.describe RevenueByProjectReport do describe "description" do it "returns a description of the reports purpose" do # verify the description end end describe "headers" do it "returns descriptive headers" do # verify the headers end end end 1 -
I tr du in a ha ed xa pl RSpec.describe
RevenueByLocationReport do it_behaves_like "a report" end RSpec.describe RevenueByProjectReport do it_behaves_like "a report" end 1 -
C ll ng S ar d xa pl include_examples "a
collection" (includes the examples in the current context) # RevenueByLocationReport # description # returns a String describing the reports purpose it_behaves_like "a collection" (includes the examples in a nested context) # RevenueByLocationReport # behaves like a report # description # returns a String describing the reports purpose 1 -
E fo ce n nt rf ce S e s
r e c r e s e s. T r o e t : 4 D t 4 E i c 1 -
E fo ce n nt rf ce class Product include
MarkdownDescription end class Location include MarkdownDescription end 2 -
E fo ce n nt rf ce module MarkdownDescription MARKDOWN_TEMPLATE
= "string://<div class='markdown'><%= @body %></div>" def description_html Kramdown::Document.new( process_pasted_content, template: MARKDOWN_TEMPLATE, ).to_html end private HEADER_MATCH = /\n\n([A-Z &]+:)\n/ HEADER_MARKDOWN = "\n\n\\1\n\n".freeze LIST_MATCH = /\n(?:•|\*)/ LIST_MARKDOWN = "\n* ".freeze def process_pasted_content description.to_s. gsub(HEADER_MATCH, HEADER_MARKDOWN). gsub(LIST_MATCH, LIST_MARKDOWN) end end 2 -
E fo ce n nt rf ce RSpec.shared_examples "a record
with markdown description" do describe "#description_html" do it "method exists" do record = described_class.new expect(record).to respond_to(:description_html) end end end 2 -
E fo ce n nt rf ce - T st
RSpec.describe Product do it_behaves_like "a record with a markdown description" end RSpec.describe Location do it_behaves_like "a record with a markdown description" end 2 -
S ar d xa pl s ev ew 4 H
r c e t r h t t v . 4 U s e s s l e i e i t 4 S s e s w a spec/support/ shared_examples/ d r (e : /spec/shared_examples/shared_example.rb) 2 -
R so rc s R S E s: - h
://r p .c /r /r -c /d / e -g /s -e s 2