Richlewis Richlewis - 5 months ago 10
Ruby Question

Ruby/Rspec assert that first 4 characters match given text

I have a timestamp

timestamp = 1466754627


And I want to assert that the first 4 characters are
1466
within a test. If i was to use

timestamp.includes('1466')


This will pass, but
1466
could appear anywhere and not necessarily the first 4 characters. How could i ensure that
1466
are the first 4 characters ?

Thanks

Answer

There's a start_with matcher:

expect('1466754627').to start_with('1466')

You might have to convert timestamp to a string via to_s if it is indeed an integer:

timestamp = 1466754627
expect(timestamp.to_s).to start_with('1466')