do it "returns a description of the reports purpose" do report = RevenueByLocationReport.new description = report.description expect(description).to eq("Revenue 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"]) end end end 4
do it "returns a description of the reports purpose" do report = RevenueByProjectReport.new description = report.description expect(description).to eq("Revenue 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"]) end end end 6
"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
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
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 When a class name is passed to describe, the class is available via the described_class method. 9
do it "references the described class" do described_class.new # => error: undefined method on nil puts described_class.class # => NilClass end end end When a string is passed to describe, described_class is nil. 10
"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 When a Class is passed to describe, an instance of the Class is available via subject 11
"explicit subject" do it "sets the subject to an instance of the class" do expect(subject).to eq([1,2,3]) end end end You can override the implicit subject by explicitly setting the subject. 13
by RSpec (or the test author) • Lazy-evaluated • Used for one-line syntax * • Used for custom matchers # * Example of one-line syntax: RSpec.describe Array do it { is_expected.to eq([]) } # is_expected calls expect(subject).to eq([]) end Let • memoized helper • Set by the test author • Lazy-evaluated • used to reduce duplication • used to create state that is reset between each example 14
"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 15
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 16
"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 17
in the current context) # RevenueByLocationReport # description # returns a String describing the reports purpose it_behaves_like "a report" (includes the example in a nested context) # RevenueByLocationReport # behaves like a report # description # returns a String describing the reports purpose it_should_behave_like "a report" (includes the example in a nested context) # RevenueByLocationReport # it should behave like a report # description # returns a String describing the reports purpose 19
by extracting test examples to a shared location • Use shared examples sparingly as they increase indirection • Shared Examples are highly general and portable • Store shared examples within a spec/support/ shared_examples/ directory (example: /spec/shared_examples/shared_example.rb) 25