es $global_variable = 1 RSpec.describe "mutating a global variable" do it "reads from a global variable" do expect($global_variable).to eq(1) end it "updates the global variable" do $global_variable = 2 expect($global_variable).to eq(2) end end # These tests fail when the second test runs first. # Global variables are best avoided because they break encapsulation. 6
module Clearance class Configuration def initialize @routes = true end def routes_enabled? @routes end end end RSpec.describe Clearance::Configuration do it "is true by default" do expect(Clearance.configuration.routes_enabled?).to be true end it "is false when routes are set to false" do # this test setup is setting state on `Clearance.config` and # unless properly reset after each test. Running these tests # out of order will cause the 'true by default' test to fail Clearance.config { |config| config.routes = false } expect(Clearance.configuration.routes_enabled?).to be false end end 7
def self.published where.not(publication_date: nil) end end Rspec.describe Book do describe ".published" do it "returns only published books" do book1 = create(:book, :published) book2 = create(:book, :published) book3 = create(:book, :draft) expect(Book.published).to eq [book1, book2] end end end 8
class Book < ApplicationRecord def self.published where.not(publication_date: nil) end end Rspec.describe Book do describe ".published" do it "returns only published books" do book1 = create(:book, :published) book2 = create(:book, :published) book3 = create(:book, :draft) expect(Book.published).to match_array [book1, book2] expect(Book.published).to contain_exactly(book1, book2) end end end 9
Co Rspec.describe Venue do describe ".upcoming_concerts" do it "returns concerts today or in the future" do Timecop.freeze do today = DateTime.current yesterday_concert = create(:concert, start_time: today - 1.day) todays_concert = create(:concert, start_time: today) todays_concert = create(:concert, start_time: today + 1.day) upcoming_concert_names = venue.upcoming_concerts.map(&:name) expect(upcoming_concerts).to match_array( [todays_concert.name, tomorrows_concert.name] ) end end end end 1
objects maintain greater precision than the database. # use the `be_within` matcher to compare timestamps expect(database_record.updated_at).to be_within(1.second).of(ruby_object.timestamp) 1