[User Management]: testUser has filed "_id"

I faced with issue in first test in topic user-management.
It looks that ‘testUser’ from test has “_id”. And in test one of steps run command ‘delete user._id’.
If this line will be removed then test case is passed.

Is it ok?

1 Like

Yeah, The same happened to me. I wonder if there is something wrong with this?!!

And When did the _id field was added to the testUser?

I think this because we didn’t create a new object and passed the original object directly which got mutated to contain _id. But I still don’t know where the _id was added.

Do a bit more than “think” that is what happened. That did happen.

So ask yourself, “How do I stop an object from being mutated?”

Maybe if you don’t want the original to be mutated, you should take a copy of that object, or at least a copy of the properties you actually need.

Do not change unit tests. If they fail, then your implementation is the thing that needs to be corrected, and not the test.

1 Like

Thanks @neillunn

I know how to copy an object. and I already updated my solution and everything is working fine and I know it’s what happened I just don’t like to provide my suggestion as a fact as I could be mistaken which happens very often and leave everything open for discussion :slight_smile:

The strange thing for me was that the mongodb driver is mutating the object. I know that the MongoDB will add the _id automatically if we don’t provide one but I thought this will happen kinda internally in the MongoDB itself and won’t affect the passed object.

Anyway, I think:joy: it’s all clear to me now.