Skip to content
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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ build/
# Package Files #
*.jar

*.log
*.log
/dot-net/UnitTesting/.vs/WriteUnitTest/v15
/dot-net/UnitTesting/WriteUnitTest/bin/Debug
/dot-net/UnitTesting/WriteUnitTest/obj/Debug
/dot-net/UnitTesting/WriteUnitTest.UnitTests/bin/Debug
/dot-net/UnitTesting/WriteUnitTest.UnitTests/obj/Debug
/dot-net/WhatWouldYouChange
Original file line number Diff line number Diff line change
@@ -1,13 +1,125 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using WriteUnitTest.Entities;
using WriteUnitTest.Repositories;
using WriteUnitTest.Services;

namespace WriteUnitTest.UnitTests.Services
{
[TestClass]
public class LessonServiceUnitTests
{
[TestMethod]
public void UpdateLessonGrade_Test()
public void UpdateLessonGrade_PassingGradeSetWithAGradeGreaterThanMinimumPassingGrade()
{
var fakeLessonRepo = new LessonRepoFake();
var fakeModuleRepo = new ModuleRepoFake();
var lessonId = 1;
var lessonService = new LessonService(fakeLessonRepo, fakeModuleRepo);

lessonService.UpdateLessonGrade(lessonId, 81);

Assert.IsTrue(fakeLessonRepo.lessonList.FirstOrDefault(x => x.LessonId == lessonId).IsPassed);
}

[TestMethod]
public void UpdateLessonGrade_PassingGradeSetWithAGradeEqualToMinimumPassingGrade()
{
var fakeLessonRepo = new LessonRepoFake();
var fakeModuleRepo = new ModuleRepoFake();
var lessonId = 1;
var lessonService = new LessonService(fakeLessonRepo, fakeModuleRepo);

lessonService.UpdateLessonGrade(lessonId, 80);

Assert.IsTrue(fakeLessonRepo.lessonList.FirstOrDefault(x => x.LessonId == lessonId).IsPassed);
}

[TestMethod]
public void UpdateLessonGrade_FailingGradeSetWithAGradeLessThanMinimumPassingGrade()
{
var fakeLessonRepo = new LessonRepoFake();
var fakeModuleRepo = new ModuleRepoFake();
var lessonId = 1;
var lessonService = new LessonService(fakeLessonRepo, fakeModuleRepo);

lessonService.UpdateLessonGrade(lessonId, 79);

Assert.IsFalse(fakeLessonRepo.lessonList.FirstOrDefault(x => x.LessonId == lessonId).IsPassed);
}

// Additional tests should include
// Invalid LessonIds
// Grades below 0 or above 100 (assuming this matches the business rules)
// Module with no MinimumPassingGrade

}


public class LessonRepoFake : ILessonRepository
{
public readonly List<Lesson> lessonList;

public LessonRepoFake()
{
lessonList = new List<Lesson>
{
new Lesson
{
LessonId = 1,
Grade = 63.7d,
IsPassed = false
},
new Lesson
{
LessonId = 9,
Grade = 0.0d,
IsPassed = false
}
};
}

public Lesson GetLesson(int lessonId)
{
return lessonList.FirstOrDefault(x => x.LessonId == lessonId);
}
}

public class ModuleRepoFake : IModuleRepository
{
public readonly List<Module> moduleList;

public ModuleRepoFake()
{
moduleList = new List<Module>
{
new Module
{
ModuleId = 873,
MinimumPassingGrade = 80,
Lessons = new List<Lesson>
{
new Lesson
{
LessonId = 1,
Grade = 63.7d,
IsPassed = false
},
new Lesson
{
LessonId = 9,
Grade = 88.2d,
IsPassed = true
}
}
}
};
}

public Module GetModule(int lessonId)
{
return moduleList.FirstOrDefault(x => x.Lessons.Any(y => y.LessonId == lessonId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<Compile Include="Services\LessonServiceUnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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,9 @@
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,7 +4,7 @@

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

Expand Down
18 changes: 14 additions & 4 deletions dot-net/UnitTesting/WriteUnitTest/Services/LessonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ namespace WriteUnitTest.Services
{
public class LessonService
{
private readonly ILessonRepository LessonRepository;
private readonly IModuleRepository ModuleRepository;


public LessonService()
{
LessonRepository = new LessonRepository();
ModuleRepository = new 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);

lesson.Grade = grade;

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

if (grade >= module.MinimumPassingGrade)
{
Expand Down
2 changes: 2 additions & 0 deletions dot-net/UnitTesting/WriteUnitTest/WriteUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<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\LessonService.cs" />
Expand Down
30 changes: 29 additions & 1 deletion dot-net/WhatWouldYouChange/INSTRUCTIONS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,32 @@


Suggested Changes:


Snippet
- Use more descriptive names, though in this case it makes some sense not to go overboard. Specifically ExampleMethod() could have been ReadExampleFile().

- Property should start with a capital letter. (ExampleText)

- Catch specific exceptions in this case at least: catch (FileNotFoundException e)

- Use using statement to handle the disposal of the File and StreamReader

- Don't concantenate the text in the catch block. use the "@" string prefix ex:
text = @"Lorem ipsum dolor
facilisis auctor
euismod nisi"

- Depending on the use case it may be worth converting the fail state (Lorem ipsum...) text into a constant and moving it into a seperate file.

- Add guard clauses for the filename parameter.
- empty
- not null
- does the file exist (though only needed if we need to do something specific on that error condition ie logging )

- Add logging, especially in the catch block.

- Don't start the catch block with text = text + "...". There is the possiblity that the text variable may contain part of the stream. I think it's unlikely but always better to be safe.

- In this case the text varaible is unnecessary assignments could be made directly to the property exampleText.

- Unless there is a reason not to, make the setter private.