Kelvin Matheus
Kelvin Matheus

Reputation: 147

Error - RSpec - expect method is escaping '\n'

I would like to assert this expectation:

contas_csv = transacoes_decorator.converter_contas_para_csv
expect(contas_csv).to eq('345,30000\n350,19500\n355,20000\n360,-31000\n')

I got this error:

 expected: "345,30000\\n350,19500\\n355,20000\\n360,-31000\\n"
      got: "345,30000\n350,19500\n355,20000\n360,-31000\n"

I printed the contas_csv variable, and I got what I expected "345,30000\n350,19500\n355,20000\n360,-31000\n"

But the RSpec is adding an extra '\' before the '\n'. Because of it, my test is failing.

Someone can help me with that, please?

Upvotes: 1

Views: 562

Answers (1)

tadman
tadman

Reputation: 211670

You're using single quotes so special characters like \n are taken literally, not expanded to their control-character counterparts.

Use this:

"...\n..."

With double quotes. '\n' is the same as "\\n".

Upvotes: 4

Related Questions