Logo: Relish

  1. Sign up
  2. Sign in

Project: RSpec Expectations 3.6

`exist` matcher

The exist matcher is used to specify that something exists (as indicated by #exist? or #exists?):

  expect(obj).to exist # passes if obj.exist? or obj.exists?
Scenarios
basic usage
Given
a file named "exist_matcher_spec.rb" with:
class Planet
  attr_reader :name

  def initialize(name)
    @name = name
  end

  def inspect
    "<Planet: #{name}>"
  end

  def exist? # also works with exists?
    %w[Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune].include?(name)
  end
end

RSpec.describe "Earth" do
  let(:earth) { Planet.new("Earth") }
  specify { expect(earth).to exist }
  specify { expect(earth).not_to exist } # deliberate failure
end

RSpec.describe "Tatooine" do
  let(:tatooine) { Planet.new("Tatooine") }
  specify { expect(tatooine).to exist } # deliberate failure
  specify { expect(tatooine).not_to exist }
end
When
I run rspec exist_matcher_spec.rb
Then
the output should contain all of these:
4 examples, 2 failures
expected <Planet: Earth> not to exist
expected <Planet: Tatooine> to exist

Last published 7 months ago by myronmarston.