From 29130330da3325a6540f09d9fda5e06686c9f0ef Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Sun, 13 Mar 2016 14:35:38 -0400 Subject: [PATCH] Finished all controller unit tests --- .../Api/IdeasController.cs | 8 +- .../InternalsVisibleTo.cs | 3 + .../TestingControllerSample.Tests.xproj | 3 + .../UnitTests/ApiIdeasControllerCreate.cs | 78 +++++++++++++++++++ .../UnitTests/ApiIdeasControllerForSession.cs | 54 +++++++++++++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/InternalsVisibleTo.cs create mode 100644 aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerCreate.cs create mode 100644 aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerForSession.cs diff --git a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/Api/IdeasController.cs b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/Api/IdeasController.cs index 7aa01a4d5d..bee58e9184 100644 --- a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/Api/IdeasController.cs +++ b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/Api/IdeasController.cs @@ -26,7 +26,13 @@ namespace TestingControllersSample.Api { return HttpNotFound(sessionId); } - return new ObjectResult(session.Ideas.Select(i => new { id = i.Id, name=i.Name, description = i.Description, dateCreated = i.DateCreated})); + return new ObjectResult(session.Ideas.Select(i => new + { + id = i.Id, + name =i.Name, + description = i.Description, + dateCreated = i.DateCreated + })); } public class NewIdeaModel diff --git a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/InternalsVisibleTo.cs b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/InternalsVisibleTo.cs new file mode 100644 index 0000000000..61a2a00bfd --- /dev/null +++ b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/src/TestingControllersSample/InternalsVisibleTo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("TestingControllerSample.Tests")] \ No newline at end of file diff --git a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/TestingControllerSample.Tests.xproj b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/TestingControllerSample.Tests.xproj index eebf07bd26..dac1b2fc4b 100644 --- a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/TestingControllerSample.Tests.xproj +++ b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/TestingControllerSample.Tests.xproj @@ -14,6 +14,9 @@ 2.0 + + True + diff --git a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerCreate.cs b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerCreate.cs new file mode 100644 index 0000000000..7ccfe6d91c --- /dev/null +++ b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerCreate.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNet.Mvc; +using Moq; +using TestingControllersSample.Api; +using TestingControllersSample.Core.Interfaces; +using TestingControllersSample.Core.Model; +using Xunit; +namespace TestingControllerSample.Tests.UnitTests +{ + public class ApiIdeasControllerCreate + { + [Fact] + public void ReturnsBadRequestGivenInvalidModel() + { + var mockRepo = new Mock(); + var controller = new IdeasController(mockRepo.Object); + controller.ModelState.AddModelError("error","some error"); + + var result = Assert.IsType(controller.Create(null)); + } + + [Fact] + public void ReturnsHttpNotFoundForInvalidSession() + { + var mockRepo = new Mock(); + int testSessionId = 123; + mockRepo.Setup(r => r.GetById(testSessionId)).Returns((BrainStormSession)null); + var controller = new IdeasController(mockRepo.Object); + + var result = Assert.IsType(controller.Create(new IdeasController.NewIdeaModel())); + } + + + [Fact] + public void ReturnsNewlyCreatedIdeaForSession() + { + var mockRepo = new Mock(); + int testSessionId = 123; + string testName = "test name"; + string testDescription = "test description"; + var testSession = GetTestSession(); + mockRepo.Setup(r => r.GetById(testSessionId)).Returns(testSession); + var controller = new IdeasController(mockRepo.Object); + + var newIdea = new IdeasController.NewIdeaModel() + { + Description = testDescription, + Name = testName, + SessionId = testSessionId + }; + mockRepo.Setup(r => r.Update(testSession)).Verifiable(); + + var result = Assert.IsType(controller.Create(newIdea)); + var returnSession = result.Value as BrainStormSession; + + mockRepo.Verify(); + Assert.Equal(2, returnSession.Ideas.Count()); + Assert.Equal(testName, returnSession.Ideas.LastOrDefault().Name); + Assert.Equal(testDescription, returnSession.Ideas.LastOrDefault().Description); + } + + private BrainStormSession GetTestSession() + { + var session = new BrainStormSession() + { + DateCreated = new DateTime(2016, 7, 2), + Id = 1, + Name = "Test One" + }; + + var idea = new Idea() {Name = "One"}; + session.AddIdea(idea); + return session; + } + } +} \ No newline at end of file diff --git a/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerForSession.cs b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerForSession.cs new file mode 100644 index 0000000000..4b63d3516d --- /dev/null +++ b/aspnet/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllerSample.Tests/UnitTests/ApiIdeasControllerForSession.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNet.Mvc; +using Moq; +using TestingControllersSample.Api; +using TestingControllersSample.Core.Interfaces; +using TestingControllersSample.Core.Model; +using Xunit; +namespace TestingControllerSample.Tests.UnitTests +{ + public class ApiIdeasControllerForSession + { + [Fact] + public void ReturnsHttpNotFoundForInvalidSession() + { + var mockRepo = new Mock(); + int testSessionId = 123; + mockRepo.Setup(r => r.GetById(testSessionId)).Returns((BrainStormSession)null); + var controller = new IdeasController(mockRepo.Object); + + var result = Assert.IsType(controller.ForSession(testSessionId)); + } + + [Fact] + public void ReturnsIdeasForSession() + { + var mockRepo = new Mock(); + int testSessionId = 123; + mockRepo.Setup(r => r.GetById(testSessionId)).Returns(GetTestSession()); + var controller = new IdeasController(mockRepo.Object); + + var result = Assert.IsType(controller.ForSession(testSessionId)).Value as IEnumerable; + dynamic idea = result.FirstOrDefault(); + + // this requires InternalsVisibleTo on the SUT project + Assert.Equal("One", idea.name); + } + + private BrainStormSession GetTestSession() + { + var session = new BrainStormSession() + { + DateCreated = new DateTime(2016, 7, 2), + Id = 1, + Name = "Test One" + }; + + var idea = new Idea() {Name = "One"}; + session.AddIdea(idea); + return session; + } + } +} \ No newline at end of file