public async Task InsertAsync_CallsInsertOneAsyncOnCollectionOnce_ThenReturnsEntityThatWasPassed() { //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 var result = await repo.InsertAsync(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.InsertOneAsync( It.IsAny <BottleMongoModel>(), It.IsAny <InsertOneOptions>(), It.IsAny <CancellationToken>()), Times.Once()); Assert.AreEqual(bottle.BottleId, result.BottleId); Assert.AreEqual(bottle.AlcoholCategory, result.AlcoholCategory); }
public async Task InsertAsync_ReturnsNullWithoutTryingToInsertIntoCollection_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 null without ever accessing the collection. _mockDbContext.Setup(c => c.Collection).Throws(new System.Exception("Test failed. MongoDbContext Collection property was accessed. InsertAsync method should return null without accessing the Collection property.")); var repo = new BottleMongoRepository(_mockDbContext.Object, _toMongoModelMapper, _toDomainModelMapper); //Act var result = await repo.InsertAsync(null); //Assert Assert.IsNull(result); }