Chapter 3 - Ticket Create/Update Comments - Possible bug in the test for update (?)

Hi. So far everything have been going well 'till I got into the update comments part, where I kept receiving from my test, no matter what I did/changed “Error Message: Object reference not set to an instance of an object”.
My query in UpdateCommentAsync is constructed as follows (which I also tried modifying many times to at least get another error message ;)) :

        return await _commentsCollection.UpdateOneAsync(
            Builders<Comment>.Filter.Where(c => c.Id == commentId && c.Email == user.Email),
            Builders<Comment>.Update.Set(c => c.Text, comment).Set(c => c.Date, DateTime.UtcNow),
            new UpdateOptions {IsUpsert = true},
            cancellationToken);

However, I had a look at the test TestUpdatesComment(), where I commented out almost everything inside of the try {} block. I left out only the first assertions:

            await AddCommentToMovieAsync(oldCommentText);
            var movie = await _movieRepository.GetMovieAsync(_movieId);
            var firstComment = movie.Comments.FirstOrDefault();
            _commentId = firstComment.Id;
            Assert.IsNotNull(firstComment);
            Assert.AreEqual(oldCommentText, firstComment.Text);

and the test still returns the same error message. I checked the movie with that id in the atlas, it doesn’t have any comments. May it be the reason why it fails? Did I miss something?

Hi @Betina_Zhou, welcome to the community :wave: .
Thank you for wrapping your implementation in [spoiler] tags.

Was there anything else that got logged in the terminal apart from the message you shared below?

Also, the following post addresses an issue that’s similar to the error you have shared:

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

an upsert operation (update or insert) requires a new object if the document does not exist. It is true you may insert any fields into a MongoDB document, but in this case, your document will have only “Text” and “Date” assigned while other fields will be null, plus the newly added “ObjectId” (if it succeeds to insert). Check how we created a new comment in AddCommentAsync function.

in your current implementation, if it is an upsert, there will be no “Name”, no “Email”, no “MovieId”, thus you will not be able to find this document back by querying “Email” or “MovieId” fields, and get a null result.

Just remove upsert option and try again.

if you still fail, then also check your filter expression. for example, you haven’t used “movieId” variable yet. It may or may not affect the result but check regardless.

@Betina_Zhou I had the same issue, basically the user is passed empty as the test itself tries to use something that doesn’t exists, you will see that parameter “User” in AddCommentAsync is null.

Is it just me or the unit test for update comments fails and passes on the status page of mflix

Try this out [spoiler]
return await _commentsCollection.UpdateOneAsync(
Builders.Filter.Where(c => c.Id == commentId && c.Email == “youkiltmyfodder@mongodb.com”),
Builders.Update.Set(c => c.Text, comment).Set(c => c.Date, DateTime.UtcNow),
new UpdateOptions() { IsUpsert = true },
cancellationToken);

[spoiler]
For me, my user was null no matter what I did so I had to change the user inputs to being manual

Very strange! Adding the following values ​​as arguments in…
Builders.Filter.Where( c => c.Id==commentId
&& c.MovieId == movieId && c.Email == user.Email)
. I can get the validation code. But in the tests it fails!

I would check everywhere you use Email. I am not too sure it is stored with an uppercase E in the collection.