using IResultImplementation.Data; using IResultImplementation.Models; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; namespace IResultImplementation.Tests { [TestClass] public class IResultImplementationTest { public IQueryable? Data { get; set; } public Mock>? MockSet { get; set; } [TestInitialize] public void Setup() { Data = new List() { new Contact() { Id = 1, Name = "John", Email = "john@tmail.com", PhoneNumber = "1234567890" }, new Contact() { Id = 2, Name = "Jane", Email = "Jane@kam.com", PhoneNumber = "29384736273" }, new Contact() {Id = 3, Name = "Kahn", Email = "ema@email.com", PhoneNumber = "23239029202"} }.AsQueryable(); MockSet = new Mock>(); MockSet.As>().Setup(m => m.Provider).Returns(Data.Provider); MockSet.As>().Setup(m => m.Expression).Returns(Data.Expression); MockSet.As>().Setup(m => m.ElementType).Returns(Data.ElementType); MockSet.As>().Setup(m => m.GetEnumerator()).Returns(() => Data.GetEnumerator()); } [TestMethod] public void GetContactsReturnsContactsFromDatabase() { //Arrange var mockContext = new Mock(); mockContext.Setup(c => c.Contact).Returns(MockSet!.Object); int expectedStatusCode = 200; int expectedItemCount = 3; //Act var result = (Ok>)ContactsHandler.GetContacts(mockContext.Object); //Assert Assert.AreEqual(expectedStatusCode, result.StatusCode); Assert.AreEqual(expectedItemCount, result.Value?.Count); } [TestMethod] public void GetContactReturnsAContactFromDatabase() { //Arrange var mockContext = new Mock(); mockContext.Setup(c => c.Contact).Returns(MockSet!.Object); int expectedStatusCode = 200; int expectedUserId = 2; //Act var result = (Ok)ContactsHandler.GetContact(mockContext.Object, expectedUserId); //Assert Assert.AreEqual(expectedStatusCode, result.StatusCode); Assert.AreEqual(expectedUserId, result.Value?.Id); } [TestMethod] public void GetContactReturnsNotFound() { //Arrange var mockContext = new Mock(); mockContext.Setup(c => c.Contact).Returns(MockSet!.Object); int expectedStatusCode = 404; int expectedUserId = 20; //Act var result = (NotFound)ContactsHandler.GetContact(mockContext.Object, expectedUserId); //Assert Assert.AreEqual(expectedStatusCode, result.StatusCode); } [TestMethod] public void CreateTodoSavesContactToDatabase() { //Arrange var mockDbContextOptions = new Mock>(); var mockContext = new Mock(); mockContext.Setup(c => c.Contact).Returns(MockSet!.Object); mockContext.Setup(c => c.Contact.Add(It.IsAny())).Callback(contact => Data = Data!.Append(contact)); var newContact = new Contact() { Id = 4, Name = "John Doe", Email = "akd@omail.com", PhoneNumber = "1234567890" }; int expectedStatusCode = 201; int expectedItemCount = 4; //Act var result = (CreatedAtRoute)ContactsHandler.PostContact(mockContext.Object, newContact); //Assert Assert.AreEqual(newContact, result.Value); Assert.AreEqual(expectedStatusCode, result.StatusCode); Assert.AreEqual(expectedItemCount, Data!.Count()); } } }