diff --git a/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs b/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs index c016f412..71b31c1d 100644 --- a/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs +++ b/dot-net/UnitTesting/WriteUnitTest.UnitTests/Services/LessonServiceUnitTests.cs @@ -1,13 +1,92 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using FakeItEasy; +using NUnit.Framework; +using WriteUnitTest.Entities; +using WriteUnitTest.Repositories; +using WriteUnitTest.Services; namespace WriteUnitTest.UnitTests.Services { - [TestClass] + [TestFixture] public class LessonServiceUnitTests { - [TestMethod] - public void UpdateLessonGrade_Test() + private ILessonService _lessonService; + private ILessonRepository _lessonRepository; + private IModuleRepository _moduleRepository; + + [SetUp] + public void Init() + { + _lessonRepository = A.Fake(); + _moduleRepository = A.Fake(); + _lessonService = new LessonService(_lessonRepository, _moduleRepository); + } + + [Test] + public void when_passing_valid_lessonid_update_lesson_grade() + { + const int lessonId = 12; + const double grade = 63.7d; + var fakeLesson = new Lesson + { + Grade = grade, + IsPassed = false, + LessonId = lessonId + }; + //Arrange + A.CallTo(() => _lessonRepository.GetLesson(lessonId)).Returns(fakeLesson); + A.CallTo(() => _moduleRepository.GetModule(lessonId)).Returns(new Module + { + Lessons = new List{ fakeLesson }, + MinimumPassingGrade = 80, + ModuleId = 873 + }); + + //Act + _lessonService.UpdateLessonGrade(lessonId, grade); + + //Assert + A.CallTo(() => _lessonRepository.GetLesson(A.Ignored)).MustHaveHappenedOnceExactly(); + A.CallTo(() => _moduleRepository.GetModule(A.Ignored)).MustHaveHappenedOnceExactly(); + } + + [Test] + public void when_passing_invalid_lessonid_to_lesson_throws_exception() + { + const int lessonId = 20; + const double grade = 20.5d; + //Arrange + A.CallTo(() => _lessonRepository.GetLesson(lessonId)).Returns(null); + + //Act + + //Assert + Assert.Throws(() => _lessonService.UpdateLessonGrade(lessonId, grade)); + A.CallTo(() => _lessonRepository.GetLesson(A.Ignored)).MustHaveHappenedOnceExactly(); + A.CallTo(() => _moduleRepository.GetModule(A.Ignored)).MustNotHaveHappened(); + } + + [Test] + public void when_passing_invalid_lessonid_to_module_throws_exception() { + //Arrange + const int lessonId = 20; + const double grade = 20.5d; + A.CallTo(() => _lessonRepository.GetLesson(lessonId)).Returns(new Lesson + { + Grade = grade, + IsPassed = false, + LessonId = lessonId + }); + A.CallTo(() => _moduleRepository.GetModule(lessonId)).Returns(null); + + //Act + + //Assert + Assert.Throws(() => _lessonService.UpdateLessonGrade(lessonId, grade)); + A.CallTo(() => _lessonRepository.GetLesson(A.Ignored)).MustHaveHappenedOnceExactly(); + A.CallTo(() => _moduleRepository.GetModule(A.Ignored)).MustHaveHappenedOnceExactly(); } } } \ 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..27317308 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,6 +38,12 @@ 4 + + ..\packages\FakeItEasy.5.1.1\lib\net45\FakeItEasy.dll + + + ..\packages\NUnit.3.11.0\lib\net45\nunit.framework.dll + @@ -55,6 +64,15 @@ + + + + + + {00A40A05-8314-4F25-A444-46DDEAC3497E} + WriteUnitTest + + @@ -75,6 +93,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}. + + +