public async Task UpdateAsync_CallsReplaceOneAsyncOnCollectionOnce_WhenValidIdAndNonNullEntityPassed() { //Arrange //Create the BottleDomainModel that we will insert into the repo. This same entity should be returned by the method on a successful run. var bottle = new BottleDomainModel { BottleId = "507f1f77bcf86cd799439010", AlcoholCategory = AlcoholCategory.Whisky }; var mockCollection = new Mock <IMongoCollection <BottleMongoModel> >(); _mockDbContext.SetupGet(c => c.Collection).Returns(mockCollection.Object); var repo = new BottleMongoRepository(_mockDbContext.Object, _toMongoModelMapper, _toDomainModelMapper); //Act await repo.UpdateAsync("507f1f77bcf86cd799439010", bottle); //Assert //Verify that the db context Collection's InsertoneAsync method is called exactly once. //Any more or less will throw meaning the test will fail. mockCollection.Verify(c => c.ReplaceOneAsync( It.IsAny <FilterDefinition <BottleMongoModel> >(), It.IsAny <BottleMongoModel>(), It.IsAny <ReplaceOptions>(), It.IsAny <CancellationToken>()), Times.Once()); }
public async Task UpdateAsync_ReturnsWithoutAccessingCollection_WhenNullBottleDomainModelPassed() { //Arrange //Set up the db context to throw if the Collection property is accessed. In this way we test that the InsertAsync method returns without ever accessing the collection. _mockDbContext.Setup(c => c.Collection).Throws(new System.Exception("Test failed. MongoDbContext Collection property was accessed. UpdateAsync method should return without accessing the Collection property.")); var repo = new BottleMongoRepository(_mockDbContext.Object, _toMongoModelMapper, _toDomainModelMapper); //Act await repo.UpdateAsync("507f1f77bcf86cd799439010", null); //Assert - assertion is just that the method returns without accessing the db context Collection property. Test will fail if the property is accessed. }