using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using ResourcesManager.Controllers; using ResourcesManager.Interfaces; using ResourcesManager.Models; using ResourcesManagerTests.RepositoryTests; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; namespace ResourcesManager.Controllers.Tests { [TestClass()] public class StatisticsControllerTests { List expectedStat; public Mock mockStatRepo; StatisticsController statController; ResourcesControllerTests resourcesContTest; UserRepositoryTests userRepoTest; public StatisticsControllerTests() { InitializeStatisticTestData(); } [TestInitialize] public void InitializeStatisticTestData() { expectedStat = new List { new StatisticModel(){ Id= 1, ResourceId = 1, ReservationId = 1, StatisticType = StatisticType.Login, DateTime = DateTime.Now, UserId = Guid.NewGuid().ToString()}, new StatisticModel(){ Id= 2, ResourceId = 2, ReservationId = 2, StatisticType = StatisticType.Reserv, DateTime = DateTime.Now, UserId = Guid.NewGuid().ToString()}, new StatisticModel(){ Id= 3, ResourceId = 3, ReservationId = 3, StatisticType = StatisticType.ResourceCreate, DateTime = DateTime.Now, UserId = Guid.NewGuid().ToString()}, }; mockStatRepo = new Mock() { CallBase = true }; resourcesContTest = new ResourcesControllerTests(); userRepoTest = new UserRepositoryTests(); statController = new StatisticsController(mockStatRepo.Object, resourcesContTest.mockResourceRepo.Object, userRepoTest.mockUserRepo.Object); mockStatRepo.Setup(m => m.GetStatistics()).Returns(expectedStat); mockStatRepo.Setup(m => m.GetStatisticByID(It.IsAny())).Returns((int id) => { return expectedStat.FirstOrDefault(x => x.Id == id); }); mockStatRepo.Setup(m => m.InsertStatistic(It.IsAny())).Returns((StatisticModel stat) => { expectedStat.Add(stat); return true; }); } [TestMethod()] public void Statistic_Get_Index_Test() { var result = statController.Index(StatisticType.Login, 1); Assert.IsNotNull((result as ViewResult).Model); } //todo: még [TestCleanup] public void Statistic_CleanUpTestData() { expectedStat = null; resourcesContTest.Resources_CleanUpTestData(); userRepoTest.User_CleanUpTestData(); } } }