public void Get_InputId_CalledFindMethodOfDBSetWithCorrectId() { DbContextOptions opt = new DbContextOptionsBuilder <RoutesInfoSystemContext>().Options; var mockContext = new Mock <RoutesInfoSystemContext>(opt); var mockDbSet = new Mock <DbSet <UnitRoute> >(); mockContext.Setup(context => context.Set <UnitRoute>()).Returns(mockDbSet.Object); UnitRoute expectedEducationMaterial = new UnitRoute() { RouteId = 1 }; mockDbSet.Setup(mock => mock.Find(expectedEducationMaterial.RouteId)).Returns(expectedEducationMaterial); var repository = new TestRoutesListRepository(mockContext.Object); var actualEducationMaterial = repository.Get(expectedEducationMaterial.RouteId); mockDbSet.Verify(dbSet => dbSet.Find(expectedEducationMaterial.RouteId), Times.Once()); Assert.Equal(expectedEducationMaterial, actualEducationMaterial); }
public void Delete_InputId_CalledFindAndRemoveMethodsOfDBSetWithCorrectArg() { DbContextOptions opt = new DbContextOptionsBuilder <RoutesInfoSystemContext>().Options; var mockContext = new Mock <RoutesInfoSystemContext>(opt); var mockDbSet = new Mock <DbSet <UnitRoute> >(); mockContext.Setup(context => context.Set <UnitRoute>()).Returns(mockDbSet.Object); var repository = new TestRoutesListRepository(mockContext.Object); UnitRoute expectedEducationMaterial = new UnitRoute() { RouteId = 1 }; mockDbSet.Setup(mock => mock.Find(expectedEducationMaterial.RouteId)).Returns(expectedEducationMaterial); repository.Delete(expectedEducationMaterial.RouteId); mockDbSet.Verify(dbSet => dbSet.Find(expectedEducationMaterial.RouteId), Times.Once()); mockDbSet.Verify(dbSet => dbSet.Remove(expectedEducationMaterial), Times.Once()); }
IRoutesListService GetRoutesListService() { var mockContext = new Mock <IUnitOfWork>(); var expectedRoutesList = new UnitRoute() { RouteId = 1, Name = "testName", Content = "testContent" }; var mockDbSet = new Mock <IRoutesRepository>(); mockDbSet.Setup(z => z.Find(It.IsAny <Func <UnitRoute, bool> >(), It.IsAny <int>(), It.IsAny <int>())).Returns(new List <UnitRoute>() { expectedRoutesList }); mockContext.Setup(context => context.RouteLists).Returns(mockDbSet.Object); IRoutesListService routesListService = new RoutesListService(mockContext.Object); return(routesListService); }