Richlewis Richlewis - 1 year ago 45
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
within a test. If i was to use


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


Answer Source

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')