[rspec-users] id getting overwritten with 0 when testing

Guyren G Howe gisborne at emailuser.net
Wed Jan 25 02:43:53 EST 2012

On Jan 24, 2012, at 11:27 PM, Julian Leviston wrote:

> On 25/01/2012, at 5:24 PM, Guyren G Howe wrote:
>> My code that saves a record works fine in development or production, or from the console. I can take the code in my test and run it in the console, and it works fine.
>> But when I run it under a model rspec, the ids are getting set to 0. I’ve traced it through to where I do:
>> <Model>.create <params>
>> where I can see that the id is what I want to set it to in params.
>> Same problem with rspec 2.6 and 2.8.
>> Don’t make me switch to Test::Unit. Anyone?
> It'd be nice to have a bit of context for this issue.
> It's most likely an issue with your model's validation… 

Not sure what else to tell you. I’ve a complex bit of logic I want to exercise that’s accepting a hierarchy of objects submitted to the application as JSON. The controller pulls it apart into a hierarchical key-value hash. I’ve a recursive operation that walks this structure, pulling out individual objects and saving them.

Everything works fine when I test it manually (e.g. in console). When I run the same sequence of operations with the same values in console (i.e. I tested it by copying the values and operations out of the spec into the console), it all works fine.

But it all fails horribly in rspec because the ids are getting overwritten with 0s. I can get to the point in my code where I hand things over to ActiveRecord, and the hash I’m giving to create is exactly what I’m after including the id value.

FWIW, the ids I’m trying to use are UUIDs.

Since I’m entirely sure the hash I’m handing to create is correct, I’m left with trying to grub around inside ActiveRecord, which I don’t look forward to. So: in what way does RSpec modify the behavior of ActiveRecord that might bear on this?

More information about the rspec-users mailing list