Kelvin Matheus Kelvin Matheus - 8 days ago 5
Ruby Question

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?

Answer

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".

Comments