[User Management]: Incorrect Test Case cause my code to fail

I notice the data below when I run the user-management test.

      user:  {
             _id: 5c6d6e5ee4bdc0608b704b15,
              name: 'Magical Mr. Mistoffelees',
              email: 'magicz@cats.com',
              password: 'somehashedpw' 
      }

       testUser:  { 
           name: 'Magical Mr. Mistoffelees',
           email: 'magicz@cats.com',
           password: 'somehashedpw',
           _id: 5c6d6e5ee4bdc0608b704b15
     }

Error
Difference:
- Expected
+ Received

      Object {
    -   "_id": "5c6d6dd745cca65fa3072e2e",
        "email": "magicz@cats.com",
        "name": "Magical Mr. Mistoffelees",
        "password": "somehashedpw",
      }`

I think the test case is incorrect or something is adding _id to the testUser object.

Commenting delete user._id causes the test to pass else it will always fail.

2 Likes

Same problem here :woman_shrugging:

2 Likes

Passes on the status page though once you have done all the requirements of this user management ticket (still fails in the tests).

All my tests are passing. See image below

Which of your test is not passing?

I didn’t comment out this in the tests, so the add user test was failing (because the _id didn’t match). But it still passed on the status page. :+1:

Okay. I had to comment it out for addUser test but no need to comment it for the sessionResult part.

Well it’s been noted that is not true either.

In all honesty, If you are seeing these sorts of problems then it’s far more likely your environment is messed up somehow.

I would strongly suggest a clean setup of your Node environment much along the lines of how I outlined in a previous post.

Again NONE of the test cases require ANY editing in order to pass, and the only thing you should be changing is within the DAO section, and one other place when instructed but that has not even come up in the course yet.

I have all tests passing ( even those not brought up yet ) without making any changes to the unit tests, so any issues are simply implementation and environment. AND as I have said several times here before, changing such things is likely to have a compound effect where a clean installation and data load simply become a necessity.

In fact the only changes I have needed to make beyond instruction were related to a 200ms connection timeout, which is simply not realistic outside of the North America region ( as I am ) and therefore needs a much more reasonable timeout number for a threshold. Pretty sure that does not even have any effect on graded answers though.

All my tests pass EXCEPT for addUser(). I have been trying everything, and it always returns undefined. It’s driving me crazy.

Simple verify the data that you are compare with the data of test. {key : value}, check if your key is the correct to compare in all the methods. The collections of sessions have this structure :
“_id” : ObjectId(“xxxx”),
“user_id” : “la66kfxh4@4lxhf.6k9”,
“jwt” : “xxxxxxxxxx”

Thanks! I will check out more carefully and see what is going on.

Hello @canelacho,

My login test couldn’t be passed but all others passed. I’ve tried several cases but couldn’t just login. what am I missing?

{user_id: email},
{$set: {jwt}},
{upsert: true}

Error message: cannot convert undefined or null object

finally I’m able to get all my test passed. I first had to edit logoutUser, getUserSession.

2 Likes

that’s correct, while you get error in the logoutUser and the getUserSession, it’ll give you error too. Good for you!

I had the same when wrote:
await users.insertOne(userInfo)

For some reason MongoDB add “_id” to userInfo object :hushed:.
Problem is gone when I clone userInfo:
await users.insertOne({…userInfo})

1 Like

I tried the same thing and for me the addUser test failed.

did the tests pass for before you edited logoutUser and getUserSession even tho they were incorrect ? because I have the same problem as you did everything passes except for loginUser and I wrote the same code as you did

@James_22203 Yeah!. been there for an hour with

3 test passed and 1 failed.

i found your discussion helpful and after completely implement all five method it works.

1 Like

I think I know why: if one does not clone like you did, then one is passing a reference to testUser (from the test suite) to the insertOne method.

The documentation for insertOne tells us (under the rubric of _id) that

_id Field

If the document does not specify an _id field, then mongod will add the _id field and assign a unique ObjectId for the document before inserting. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.

So I think that we get this spurious _id field inserted when we pass the object reference. Does this make sense to you? Try not cloning the object and then doing a console.log on testUser: you’ll see the _id field being inserted. The assertion will fail because the expected value will have the id but the supplied one will not (because we just deleted it in the line above). Tricky!

Test ‘user-management’ fails. The ticket passes though. Though not overly concerned would like to know the cause of the error:

Please check your UsersDAO.getUserSession function. It should return an object instead of null.

Kanika