Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored the code for Unit testing and added unit test cases along … #282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ILessonRepository>();
_moduleRepository = A.Fake<IModuleRepository>();
_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<Lesson>{ fakeLesson },
MinimumPassingGrade = 80,
ModuleId = 873
});

//Act
_lessonService.UpdateLessonGrade(lessonId, grade);

//Assert
A.CallTo(() => _lessonRepository.GetLesson(A<int>.Ignored)).MustHaveHappenedOnceExactly();
A.CallTo(() => _moduleRepository.GetModule(A<int>.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<NullReferenceException>(() => _lessonService.UpdateLessonGrade(lessonId, grade));
A.CallTo(() => _lessonRepository.GetLesson(A<int>.Ignored)).MustHaveHappenedOnceExactly();
A.CallTo(() => _moduleRepository.GetModule(A<int>.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<NullReferenceException>(() => _lessonService.UpdateLessonGrade(lessonId, grade));
A.CallTo(() => _lessonRepository.GetLesson(A<int>.Ignored)).MustHaveHappenedOnceExactly();
A.CallTo(() => _moduleRepository.GetModule(A<int>.Ignored)).MustHaveHappenedOnceExactly();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit.3.11.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.11.0\build\NUnit.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -16,6 +17,8 @@
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -35,6 +38,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="FakeItEasy, Version=5.0.0.0, Culture=neutral, PublicKeyToken=eff28e2146d5fd2c, processorArchitecture=MSIL">
<HintPath>..\packages\FakeItEasy.5.1.1\lib\net45\FakeItEasy.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.11.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.11.0\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
Expand All @@ -55,6 +64,15 @@
<Compile Include="Services\LessonServiceUnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WriteUnitTest\WriteUnitTest.csproj">
<Project>{00A40A05-8314-4F25-A444-46DDEAC3497E}</Project>
<Name>WriteUnitTest</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand All @@ -75,6 +93,12 @@
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\NUnit.3.11.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.11.0\build\NUnit.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
5 changes: 5 additions & 0 deletions dot-net/UnitTesting/WriteUnitTest.UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FakeItEasy" version="5.1.1" targetFramework="net461" />
<package id="NUnit" version="3.11.0" targetFramework="net461" />
</packages>
11 changes: 10 additions & 1 deletion dot-net/UnitTesting/WriteUnitTest/INSTRUCTIONS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ Background Information:
written and run by software developers to ensure that code meets its design
and behaves as intended.

Module - a collection of lessons
Module - a collection of lessons


Design Decision:
I have added interfaces for each class LessonRepository, ModuelRepository and LessonService
to help me implementing Dependency Injection DI. rather than directly instantiating dependencies,
or the objects that are needed for class to perform its operation are provided to the class
in abstracted way. It reduces dependencies and our code is loosely coupled. This helps in unit
testing our code and following the TDD approach.
I have added unit test cases by using NUnit and FakeItEasy framework
5 changes: 3 additions & 2 deletions dot-net/UnitTesting/WriteUnitTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using WriteUnitTest.Services;
using WriteUnitTest.Repositories;
using WriteUnitTest.Services;

namespace WriteUnitTest
{
public class Program
{
public static void Main(string[] args)
{
var lessonSvc = new LessonService();
var lessonSvc = new LessonService(new LessonRepository(), new ModuleRepository());

var lessonId = 12;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WriteUnitTest.Entities;

namespace WriteUnitTest.Repositories
{
public interface ILessonRepository
{
Lesson GetLesson(int lessonId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WriteUnitTest.Entities;

namespace WriteUnitTest.Repositories
{
public interface IModuleRepository
{
Module GetModule(int lessonId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace WriteUnitTest.Repositories
{
public class LessonRepository
public class LessonRepository : ILessonRepository
{
private readonly List<Lesson> lessonList;
private readonly List<Lesson> _lessonList;

public LessonRepository()
{
lessonList = new List<Lesson>
_lessonList = new List<Lesson>
{
new Lesson
{
Expand All @@ -29,7 +29,7 @@ public LessonRepository()

public Lesson GetLesson(int lessonId)
{
return lessonList.FirstOrDefault(x => x.LessonId == lessonId);
return _lessonList.FirstOrDefault(x => x.LessonId == lessonId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace WriteUnitTest.Repositories
{
public class ModuleRepository
public class ModuleRepository : IModuleRepository
{
private readonly List<Module> moduleList;
private readonly List<Module> _moduleList;

public ModuleRepository()
{
moduleList = new List<Module>
_moduleList = new List<Module>
{
new Module
{
Expand All @@ -37,7 +37,7 @@ public ModuleRepository()

public Module GetModule(int lessonId)
{
return moduleList.FirstOrDefault(x => x.Lessons.Any(y => y.LessonId == lessonId));
return _moduleList.FirstOrDefault(x => x.Lessons.Any(y => y.LessonId == lessonId));
}
}
}
13 changes: 13 additions & 0 deletions dot-net/UnitTesting/WriteUnitTest/Services/ILessonService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WriteUnitTest.Services
{
public interface ILessonService
{
void UpdateLessonGrade(int lessonId, double grade);
}
}
33 changes: 14 additions & 19 deletions dot-net/UnitTesting/WriteUnitTest/Services/LessonService.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
using WriteUnitTest.Repositories;
using System;
using WriteUnitTest.Repositories;

namespace WriteUnitTest.Services
{
public class LessonService
public class LessonService : ILessonService
{
public LessonService()
private readonly ILessonRepository _lessonRepository;
private readonly IModuleRepository _moduleRepository;
public LessonService(ILessonRepository lessonRepository, IModuleRepository moduleRepository)
{
_lessonRepository = lessonRepository;
_moduleRepository = moduleRepository;
}

public void UpdateLessonGrade(int lessonId, double grade)
{
var lessonRepo = new LessonRepository();
var lesson = lessonRepo.GetLesson(lessonId);

var lesson = _lessonRepository.GetLesson(lessonId);
if (lesson == null) throw new NullReferenceException();
lesson.Grade = grade;

if (!lesson.IsPassed)
{
var moduleRepository = new ModuleRepository();
var module = moduleRepository.GetModule(lessonId);
if (lesson.IsPassed) return;

if (grade >= module.MinimumPassingGrade)
{
lesson.IsPassed = true;
}
else
{
lesson.IsPassed = false;
}
}
var module = _moduleRepository.GetModule(lessonId);
if (module == null) throw new NullReferenceException();
lesson.IsPassed = grade >= module.MinimumPassingGrade;
}
}
}
3 changes: 3 additions & 0 deletions dot-net/UnitTesting/WriteUnitTest/WriteUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@
<Compile Include="Entities\Module.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\ILessonRepository.cs" />
<Compile Include="Repositories\IModuleRepository.cs" />
<Compile Include="Repositories\LessonRepository.cs" />
<Compile Include="Repositories\ModuleRepository.cs" />
<Compile Include="Services\ILessonService.cs" />
<Compile Include="Services\LessonService.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading