diff --git a/.gitignore b/.gitignore index 6c00c877..330144c7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,10 @@ build/ # Package Files # *.jar -*.log \ No newline at end of file +*.log +/.vs +/dot-net/UnitTesting +/dot-net/WhatWouldYouChange/obj +/dot-net/WhatWouldYouChange/bin/Debug +/dot-net/WhatWouldYouChange/.vs/ExampleProgram/v15 +/dot-net/.vs diff --git a/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs b/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs index c016f412..f99d26e9 100644 --- a/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs +++ b/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs @@ -1,13 +1,100 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using FizzWare.NBuilder; +using Moq; +using NUnit.Framework; +using WriteUnitTest.Entities; +using WriteUnitTest.Interfaces; +using WriteUnitTest.Services; namespace WriteUnitTest.UnitTests.Services { - [TestClass] + [TestFixture] public class LessonServiceUnitTests { - [TestMethod] - public void UpdateLessonGrade_Test() + [Test] + public void UpdateLessonGrade_LessonReturnedIsPassed_ShouldSetLessonIsPassedToTrue() { + //Arrange + var mockLessonRepository = new Mock(); + var mockModuleRepository = new Mock(); + var service = new LessonService(mockLessonRepository.Object, mockModuleRepository.Object); + //var testLesson = Builder.CreateNew().Build(); + Lesson testLesson = new Lesson { + LessonId = 1, + Grade = 70.5, + IsPassed = true + }; + mockLessonRepository.Setup(q => q.GetLesson(It.IsAny())).Returns(testLesson); + + //Act + var lesson = service.UpdateLessonGrade(testLesson.LessonId, 80.5); + + //Assert + NUnit.Framework.Assert.That(lesson, Is.Not.Null); + Assert.That(lesson.IsPassed, Is.True); + mockLessonRepository.Verify(x => x.GetLesson(It.Is(q=>q==testLesson.LessonId)), Times.Once, "Missing mandatory call to lesson repository to get lesson."); + mockModuleRepository.Verify(x => x.GetModule(It.IsAny()), Times.Never, "Invalid call made to module repository to get module."); + } + + [Test] + public void UpdateLessonGrade_LessonReturnedIsFailed_ShouldSetLessonIsPassedToFalse() + { + //Arrange + var mockLessonRepository = new Mock(); + var mockModuleRepository = new Mock(); + var service = new LessonService(mockLessonRepository.Object, mockModuleRepository.Object); + //var testLesson = Builder.CreateNew().Build(); + Lesson testLesson = new Lesson + { + LessonId = 1, + Grade = 70.5, + IsPassed = false + }; + mockLessonRepository.Setup(q => q.GetLesson(It.IsAny())).Returns(testLesson); + Module testModule = new Module + { + MinimumPassingGrade = 75 + }; + mockModuleRepository.Setup(q => q.GetModule(It.IsAny())).Returns(testModule); + + //Act + var lesson = service.UpdateLessonGrade(testLesson.LessonId, 74); + + //Assert + NUnit.Framework.Assert.That(lesson, Is.Not.Null); + Assert.That(lesson.IsPassed, Is.False); + mockLessonRepository.Verify(x => x.GetLesson(It.Is(q => q == testLesson.LessonId)), Times.Once, "Missing mandatory call to lesson repository to get lesson."); + mockModuleRepository.Verify(x => x.GetModule(It.IsAny()), Times.Once, "Missing mandatory call to module repository to get module."); + } + + [Test] + public void UpdateLessonGrade_LessonReturnedIsFailed_ShouldSetLessonIsPassedToTrue() + { + //Arrange + var mockLessonRepository = new Mock(); + var mockModuleRepository = new Mock(); + var service = new LessonService(mockLessonRepository.Object, mockModuleRepository.Object); + //var testLesson = Builder.CreateNew().Build(); + Lesson testLesson = new Lesson + { + LessonId = 1, + Grade = 70.5, + IsPassed = false + }; + mockLessonRepository.Setup(q => q.GetLesson(It.IsAny())).Returns(testLesson); + Module testModule = new Module + { + MinimumPassingGrade = 70 + }; + mockModuleRepository.Setup(q => q.GetModule(It.IsAny())).Returns(testModule); + + //Act + var lesson = service.UpdateLessonGrade(testLesson.LessonId, 74); + + //Assert + NUnit.Framework.Assert.That(lesson, Is.Not.Null); + Assert.That(lesson.IsPassed, Is.True); + mockLessonRepository.Verify(x => x.GetLesson(It.Is(q => q == testLesson.LessonId)), Times.Once, "Missing mandatory call to lesson repository to get lesson."); + mockModuleRepository.Verify(x => x.GetModule(It.IsAny()), Times.Once, "Missing mandatory call to module repository to get module."); } } } \ No newline at end of file diff --git a/dot-net/UnitTesting/WriteUnitTest.UnitTests/WriteUnitTest.UnitTests.csproj b/dot-net/UnitTesting/WriteUnitTest.UnitTests/WriteUnitTest.UnitTests.csproj index f3d83f84..76128198 100644 --- a/dot-net/UnitTesting/WriteUnitTest.UnitTests/WriteUnitTest.UnitTests.csproj +++ b/dot-net/UnitTesting/WriteUnitTest.UnitTests/WriteUnitTest.UnitTests.csproj @@ -1,5 +1,6 @@  + Debug AnyCPU @@ -16,6 +17,8 @@ $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest + + true @@ -35,7 +38,26 @@ 4 + + ..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll + + + ..\packages\NBuilder.6.0.0\lib\net40\FizzWare.NBuilder.dll + + + ..\packages\Moq.4.10.1\lib\net45\Moq.dll + + + ..\packages\NUnit.3.11.0\lib\net45\nunit.framework.dll + + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + @@ -55,6 +77,15 @@ + + + + + + {00a40a05-8314-4f25-a444-46ddeac3497e} + WriteUnitTest + + @@ -75,6 +106,12 @@ + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + +