Logo: Relish

  1. Sign up
  2. Sign in

Project: RSpec Expectations 2.14

start_with matcher

Use the start_with matcher to specify that a string or array starts with
the expected characters or elements.

  "this string".should start_with("this")
  "this string".should_not start_with("that")
  [0,1,2].should start_with(0, 1)
Scenarios
with a string
Given
a file named "example_spec.rb" with:
describe "this string" do
  it { should start_with "this" }
  it { should_not start_with "that" }

  # deliberate failures
  it { should_not start_with "this" }
  it { should start_with "that" }
end
When
I run rspec example_spec.rb
Then
the output should contain all of these:
4 examples, 2 failures
expected "this string" not to start with "this"
expected "this string" to start with "that"
with an array
Given
a file named "example_spec.rb" with:
describe [0, 1, 2, 3, 4] do
  it { should start_with 0 }
  it { should start_with(0, 1)}
  it { should_not start_with(2) }
  it { should_not start_with(0, 1, 2, 3, 4, 5) }

  # deliberate failures
  it { should_not start_with 0 }
  it { should start_with 3 }
end
When
I run rspec example_spec.rb
Then
the output should contain all of these:
6 examples, 2 failures
expected [0, 1, 2, 3, 4] not to start with 0
expected [0, 1, 2, 3, 4] to start with 3

Last published over 2 years ago by myronmarston.