Forum: Ruby on Rails issue with serialized fields and testing

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D74cc7f3b588c1933e5417b99bb1ed59?d=identicon&s=25 Chris Warren (Guest)
on 2009-06-06 03:49
(Received via mailing list)
Hi all,

I have a work-around for my problem, but there's clearly something I
don't quite understand going on here and I'd like to learn.

I have a model that has a serialized field. In the unit test for that
model I'm checking the field contents. In my fixture I have

  items: "<%=[3,8].to_yaml%>"

(thanks to Fred earlier for helping me get the fixtures loading
without error - needed the quotes around the yaml)

and in the test I have

assert things(:foo).items == [3,8]

and... the test fails. The problem is that things(:foo).items ends up
being an array that has a single element, the string '3 - 8'. For
longer arrays the string becomes '3 - 8 - 1 - 2 - 3', for example.

I can get around this by explicitly stating the yaml string as

  manage_domains: "--- \n- 3\n- 8"

which leads to the assertion being true... but... it's inelegant, and
I don't understand why the workaround is even necessary. It would be a
lot easier to use and understand if I could get the first version
working. From a couple of tests (printing to stdout) I think .to_yaml
should be putting in the new-lines, but it seems not to be in this

Anyone care to enlighten me as to why the first version doesn't work,

  -Chris W
This topic is locked and can not be replied to.