public void GetAllTopics_ExpectOk() { ICollection <TopicEntity> topics = new List <TopicEntity>(); topics.Add(topic); IEnumerable <TopicEntity> topicsEnum = topics; topicLogic.Setup(a => a.GetAll()).Returns(topicsEnum); ICollection <TopicModelOut> expected = new List <TopicModelOut>() { topicModelOut }; var result = topicController.Get(); var createdResult = result as OkObjectResult; var resultModelOut = createdResult.Value as List <TopicModelOut>; topicLogic.VerifyAll(); Assert.IsNotNull(result); Assert.IsNotNull(createdResult); Assert.IsNotNull(resultModelOut); Assert.AreEqual(expected.Count, resultModelOut.Count); Assert.AreEqual(200, createdResult.StatusCode); }
public void TopicController_Get_WhenTheIdRequestedExists_WillReturnATopic() { TopicController.GetTopicsCollection = () => { return(new List <Topic> { new Topic { topic = "ASP.NET Core", id = 1 }, new Topic { topic = "Docker for .NET Developers", id = 2 } }); }; TopicController.Init(); var controller = new TopicController(); SetUpHttpRequestParameters(controller); var response = controller.Get(1); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Topic topic; Assert.IsTrue(response.TryGetContentValue(out topic)); Assert.AreEqual("ASP.NET Core", topic.topic); Assert.AreEqual(1, topic.id); }
public void TopicController_Get_WhenTopicsCollectionIsInitializedWith2Topics_WillReturn2Topics() { TopicController.GetTopicsCollection = () => { return(new List <Topic> { new Topic { topic = "ASP.NET Core", id = 1 }, new Topic { topic = "Docker for .NET Developers", id = 2 } }); }; TopicController.Init(); var controller = new TopicController(); SetUpHttpRequestParameters(controller); var response = controller.Get(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); IEnumerable <dynamic> topics; Assert.IsTrue(response.TryGetContentValue(out topics)); var topicsArray = topics.ToArray(); var expandoDict0 = (IDictionary <string, object>)topicsArray[0]; Assert.AreEqual("ASP.NET Core", expandoDict0["topic"]); Assert.AreEqual(1, expandoDict0["id"]); var expandoDict1 = (IDictionary <string, object>)topicsArray[1]; Assert.AreEqual("Docker for .NET Developers", expandoDict1["topic"]); Assert.AreEqual(2, expandoDict1["id"]); }
public void TopicController_Get_WhenTheIdOrNameRequestedDoesNotExists_WillReturnA404() { TopicController.GetTopicsCollection = () => { return(new List <Topic> { new Topic { topic = "ASP.NET Core", id = 1 }, new Topic { topic = "Docker for .NET Developers", id = 2 } }); }; TopicController.Init(); var controller = new TopicController(); SetUpHttpRequestParameters(controller); var response = controller.Get(3, "Some Random Name"); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); ExpandoObject expando; Assert.IsTrue(response.TryGetContentValue(out expando)); var expandoDict = (IDictionary <string, object>)expando; Assert.AreEqual("The tutorial you requested was not found", expandoDict["message"]); }
public void TopicController_Get_WhenTheRepositoryThrowsAnException_ItWillBubbleUp() { _topicsRepository.SetupGet(g => g.All) .Throws(new Exception()); var controller = new TopicController(_topicsRepository.Object); SetUpHttpRequestParameters(controller); Assert.Throws <Exception>(() => controller.Get()); }
public void TopicController_Get_WhenTheRepositoryDoesNotFindATopicForTheGivenIdAndTuorialName_WillReturnA404() { _topicsRepository.Setup(g => g.Where(It.IsAny <Expression <Func <Topic, bool> > >())).Returns(new List <Topic> { }); var controller = new TopicController(_topicsRepository.Object); SetUpHttpRequestParameters(controller); var response = controller.Get(1, "ASP.NET Core on Ubuntu"); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); ExpandoObject expando; Assert.IsTrue(response.TryGetContentValue(out expando)); var expandoDict = (IDictionary <string, object>)expando; Assert.AreEqual("The tutorial you requested was not found", expandoDict["message"]); }
public void TopicController_Get_WhenTheRepositoryDoesNotFindARequestedTopic_WillReturnA404() { _topicsRepository.Setup(g => g.Find(It.IsAny <int>())) .Returns((Topic)null) .Verifiable(); var controller = new TopicController(_topicsRepository.Object); SetUpHttpRequestParameters(controller); var response = controller.Get(3); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); ExpandoObject expando; Assert.IsTrue(response.TryGetContentValue(out expando)); var expandoDict = (IDictionary <string, object>)expando; Assert.AreEqual("The topic you requested was not found", expandoDict["message"]); }
public void TopicController_Get_WhenTheRepositoryFindsATopicForTheGivenIdAndTutorialName_WillReturnATopic() { var topics = new List <Topic> { new Topic { Name = "ASP.NET Core", Id = 1, Tutorials = new List <Tutorial> { new Tutorial { Name = "ASP.NET Core on Ubuntu", Type = "video", Url = "http://www.learninghabits.co.za/#/topics/ubuntu" } } }, new Topic { Name = "Docker for .NET Developers", Id = 2 } }; _topicsRepository.Setup(g => g.Where(It.IsAny <Expression <Func <Topic, bool> > >())).Returns(topics); var controller = new TopicController(_topicsRepository.Object); SetUpHttpRequestParameters(controller); var response = controller.Get(1, "ASP.NET Core on Ubuntu"); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); ExpandoObject expando; Assert.IsTrue(response.TryGetContentValue(out expando)); var expandoDict = (IDictionary <string, object>)expando; Assert.AreEqual(1, expandoDict["id"]); var tutorials = expandoDict["tutorials"] as IEnumerable <dynamic>; Assert.IsNotNull(tutorials); var tutorialArray = tutorials.ToArray(); Assert.AreEqual(1, tutorialArray.Length); Assert.AreEqual("ASP.NET Core on Ubuntu", ((IDictionary <string, object>)tutorialArray[0])["name"]); }
public void TopicController_Get_WhenTheIdAndNameRequestedExists_WillReturnATopic() { TopicController.GetTopicsCollection = () => { return(new List <Topic> { new Topic { topic = "ASP.NET Core", id = 1, tutorials = (new List <Tutorial> { new Tutorial { name = "ASP.NET Core on Ubuntu", type = "video", url = "http://www.learninghabits.co.za/#/topics/ubuntu" } }).ToArray() }, new Topic { topic = "Docker for .NET Developers", id = 2 } }); }; TopicController.Init(); var controller = new TopicController(); SetUpHttpRequestParameters(controller); var response = controller.Get(1, "ASP.NET Core on Ubuntu"); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); ExpandoObject expando; Assert.IsTrue(response.TryGetContentValue(out expando)); var expandoDict = (IDictionary <string, object>)expando; Assert.AreEqual(1, expandoDict["id"]); var tutorials = expandoDict["tutorials"] as List <Tutorial>; Assert.IsNotNull(tutorials); Assert.AreEqual(1, tutorials.Count); Assert.AreEqual("ASP.NET Core on Ubuntu", tutorials[0].name); }
public void TopicController_Get_WhenTheRepositoryFindsARequestedTopic_WillReturnATopic() { _topicsRepository.Setup(g => g.Find(It.IsAny <int>())) .Returns(new Topic { Name = "ASP.NET Core", Id = 1 }) .Verifiable(); var controller = new TopicController(_topicsRepository.Object); SetUpHttpRequestParameters(controller); var response = controller.Get(1); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); ExpandoObject expando; Assert.IsTrue(response.TryGetContentValue(out expando)); var expandoDict = (IDictionary <string, object>)expando; Assert.AreEqual("ASP.NET Core", expandoDict["name"]); Assert.AreEqual(1, expandoDict["id"]); _topicsRepository.Verify(); }
public void TopicController_Get_WhenTheRepositoryReturns2Topics_WillReturn2Topics() { var topics = new List <Topic> { new Topic { Name = "ASP.NET Core", Id = 1 }, new Topic { Name = "Docker for .NET Developers", Id = 2 } } .AsQueryable(); _topicsRepository.SetupGet(g => g.All) .Returns(topics) .Verifiable(); var controller = new TopicController(_topicsRepository.Object); SetUpHttpRequestParameters(controller); var response = controller.Get(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); IEnumerable <dynamic> resultData; Assert.IsTrue(response.TryGetContentValue(out resultData)); var topicsArray = resultData.ToArray(); var expandoDict0 = (IDictionary <string, object>)topicsArray[0]; Assert.AreEqual("ASP.NET Core", expandoDict0["name"]); Assert.AreEqual(1, expandoDict0["id"]); var expandoDict1 = (IDictionary <string, object>)topicsArray[1]; Assert.AreEqual("Docker for .NET Developers", expandoDict1["name"]); Assert.AreEqual(2, expandoDict1["id"]); _topicsRepository.Verify(); }
public void GetReturnsNoContent() { var result = controller.Get(); Assert.IsType <NoContentResult>(result.Result); }