using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using ResourcesManager.Controllers; using ResourcesManager.Interfaces; using ResourcesManager.Models; 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 NotificationsControllerTests { public List expectedNotifications; public Mock mockNotifRepo; NotificationsController notifController; public NotificationsControllerTests() { InitializeTestData(); } [TestInitialize] public void InitializeTestData() { expectedNotifications = new List { new Notification() { Id=1, Title = "Cím", Description = "text text" }, new Notification() { Id=2, Title = "Cím", Description = "text text", Readed = false }, new Notification() { Id=3, Title = "Cím", Description = "text text" } }; mockNotifRepo = new Mock() { CallBase = true }; notifController = new NotificationsController(mockNotifRepo.Object); mockNotifRepo.Setup(m => m.GetNotifications()).Returns(expectedNotifications); mockNotifRepo.Setup(m => m.GetNotificationByID(It.IsAny())).Returns((int id) => { return expectedNotifications.FirstOrDefault(x => x.Id == id); }); mockNotifRepo.Setup(m => m.InsertNotification(It.IsAny())).Returns((Notification notif) => { expectedNotifications.Add(notif); return true; }); mockNotifRepo.Setup(m => m.UpdateNotification(It.IsAny())).Returns((Notification target) => { var notif = expectedNotifications.FirstOrDefault(x => x.Id == target.Id); if (notif == null) { return false; } notif.Title = target.Title; notif.Description = target.Description; notif.Readed = target.Readed; notif.Logo = target.Logo; notif.Link = target.Link; notif.User = target.User; notif.CreateDate = target.CreateDate; return true; }); mockNotifRepo.Setup(m => m.DeleteNotification(It.IsAny())).Returns((int id) => { var notif = expectedNotifications.FirstOrDefault(x => x.Id == id); if (notif == null) { return false; } expectedNotifications.Remove(notif); return true; }); } [TestMethod()] public void Notification_Unread_Test() { var result = notifController.GetUnread(); Assert.IsNotNull((result as JsonResult).Data); } [TestMethod()] public void Notification_Readed_Test() { notifController.Readed(1); var result = expectedNotifications.FirstOrDefault(n => n.Id == 1); Assert.AreEqual(result.Readed, true); } [TestMethod()] public void Notification_MakeReadAll_Test() { notifController.MakeReadAll(); var result = expectedNotifications.All(n => n.Readed == true); Assert.IsTrue(result); } [TestCleanup] public void Notifications_CleanUpTestData() { expectedNotifications = null; mockNotifRepo = null; } } }