public void DeleteElement_Valid() { var dbContext = new Mock <IDbContext>(); var logger = new Mock <ILogger <BaseDbService <Recipe> > >(); BaseDbService <Recipe> recipe = new BaseDbService <Recipe>(dbContext.Object, logger.Object); var result = recipe.DeleteElement(new Recipe()); dbContext.Verify(c => c.Remove(It.IsAny <Recipe>()), Times.Once); dbContext.Verify(c => c.SaveChanges(), Times.Once); logger.Verify(l => l.Log(It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Never); Assert.True(result); }
public void DeleteElement_SaveChanges_Throw_Exception() { var dbContext = new Mock <IDbContext>(); dbContext.Setup(d => d.SaveChanges()).Throws(new Exception()); var logger = new Mock <ILogger <BaseDbService <Recipe> > >(); BaseDbService <Recipe> recipe = new BaseDbService <Recipe>(dbContext.Object, logger.Object); var result = recipe.DeleteElement(new Recipe()); dbContext.Verify(a => a.Remove(It.IsAny <Recipe>()), Times.Once); dbContext.Verify(a => a.SaveChanges(), Times.Once); logger.Verify(l => l.Log(It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Once); Assert.False(result); }