0

I am writing a test and verifying some data. It's failing due to the two \\ I get in the expected data string.

My test code is:

actual_string.should eq 'Today is Tuesday.\n It is third day of the week.'

When I execute this code, I get an error saying the actual data does not match the expected data.

The actual data is:

'Today is Tuesday.\n It is third day of the week.'

The expected data is:

'Today is Tuesday.\\n It is third day of the week.'

Not sure from where is that extra slash '\' is coming from in the expected data. How can I resolve this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
tech_human
  • 6,592
  • 16
  • 65
  • 107

2 Answers2

1

use "Text" - double quotes....

edymerchk
  • 1,203
  • 13
  • 19
0

Unlike other languages (e.g. Python, JavaScript etc.), Ruby uses different escape sequences in single-quoted and double-quoted strings.

Single-quoted strings only support \' and \\. Everything else is treated literally. So, '\n' is two characters \ and n, not a single new line character.

To use the new line character, enclose your string into double quotes:

actual_string.should eq "Today is Tuesday.\n It is third day of the week."

This will fix your test.

Sergey Bolgov
  • 806
  • 5
  • 6