Skip to content

Expectation example

Cyril Kato edited this page Nov 1, 2026 · 2 revisions

expect wraps a value, or a block whose value or exception the matcher judges; to passes when the matcher matches, not_to when it does not. Each expectation writes one line, with the actual value as its inspect wrote it.

require "r_spec"

RSpec.describe "an expectation" do
  it { expect(false).not_to be true }
  it { expect("foo").to eq "foo" }
  it { expect { 1 / 0 }.to raise_exception ZeroDivisionError }
end
$ ruby spec/expectation_spec.rb
spec/expectation_spec.rb:4: Success: expected false not to be true
spec/expectation_spec.rb:5: Success: expected "foo" to eq "foo"
spec/expectation_spec.rb:6: Success: expected #<ZeroDivisionError: divided by 0> to raise exception ZeroDivisionError

Clone this wiki locally