Skip to content

Instantly share code, notes, and snippets.

@aivascu
Last active March 25, 2016 11:02
Show Gist options
  • Save aivascu/c927114e89c3b0da3c3f to your computer and use it in GitHub Desktop.
Save aivascu/c927114e89c3b0da3c3f to your computer and use it in GitHub Desktop.
Refactored unit tests
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using NSubstitute;
using NUnit.Framework;
using ProjectPersons.Business.Engines;
using ProjectPersons.Business.Helpers.Abstract;
using ProjectPersons.Business.Helpers.Concrete;
using ProjectPersons.Data.Abstract;
using ProjectPersons.Data.Entities;
namespace ProjectPersons.UnitTest
{
[TestFixture]
public class EmployeeBusinessEngineRefactoredTests
{
private static object[] _incomeCases = {
new object[] { 2, 2016, 5, 21635.7372m },
new object[] { 4, 2016, 4, 15024.8175m },
new object[] { 3, 2016, 11, 24325.895m },
new object[] { 1, 2016, 10, 32052.944m }
};
private static object[] _projectCases = {
new object[] { new DateTime(2016, 5, 3), new DateTime(2016, 5, 20), new[] { 1, 3, 5, 7 } },
new object[] { new DateTime(2017, 1, 9), new DateTime(2017, 6, 10), new[] { 1, 2, 3, 4, 5, 6, 7 } },
new object[] { new DateTime(2016, 10, 1), new DateTime(2017, 11, 1), new[] { 2, 4, 7 } },
new object[] { new DateTime(2016, 2, 1), new DateTime(2016, 2, 23), new[] { 1, 2, 3, 4, 5, 7 } }
};
private IProjectPersonsDbContext _dbContext;
[SetUp]
public void Init()
{
var employees = new List<Employee> {
new Employee { Id = 1, FirstName = "John", LastName = "Doe" },
new Employee { Id = 2, FirstName = "Bob", LastName = "Symmens" },
new Employee { Id = 3, FirstName = "Lynda", LastName = "Johnson" },
new Employee { Id = 4, FirstName = "Nataly", LastName = "Smith" },
new Employee { Id = 5, FirstName = "Harry", LastName = "Brown" },
new Employee { Id = 6, FirstName = "Kevin", LastName = "Williams" },
new Employee { Id = 7, FirstName = "Malcolm", LastName = "Freezer" }
}.AsQueryable();
var projects = new List<Project>
{
new Project
{
Id = 1,
Name = "Hotels Web Portal",
StartDate = new DateTime(2016, 4, 10),
EndDate = new DateTime(2016, 6, 20),
EmployeeAssignments = new List<EmployeeAssignment>
{
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 4), StartDate = new DateTime(2016, 4, 10), EndDate = new DateTime(2016, 6, 20), CostOfJob = 35.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 2), StartDate = new DateTime(2016, 4, 10), EndDate = new DateTime(2016, 5, 6), CostOfJob = 45.0m }
}
},
new Project
{
Id = 2,
Name = "Desktop Web App",
StartDate = new DateTime(2016, 2, 23),
EndDate = new DateTime(2016, 10, 1),
EmployeeAssignments = new List<EmployeeAssignment>
{
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 6), StartDate = new DateTime(2016, 2, 23), EndDate = new DateTime(2016, 10, 1), CostOfJob = 77.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 2), StartDate = new DateTime(2016, 5, 12), EndDate = new DateTime(2016, 5, 20), CostOfJob = 24.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 2), StartDate = new DateTime(2016, 5, 23), EndDate = new DateTime(2016, 5, 30), CostOfJob = 60.5m }
}
},
new Project
{
Id = 3,
Name = "Desktop Enterprise Application",
StartDate = new DateTime(2016, 9, 2),
EndDate = new DateTime(2016, 11, 14),
EmployeeAssignments = new List<EmployeeAssignment>
{
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 5), StartDate = new DateTime(2016, 9, 2), EndDate = new DateTime(2016, 11, 14), CostOfJob = 70.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 3), StartDate = new DateTime(2016, 9, 2), EndDate = new DateTime(2016, 11, 14), CostOfJob = 85.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 1), StartDate = new DateTime(2016, 10, 3), EndDate = new DateTime(2016, 10, 20), CostOfJob = 80.0m }
}
}
}.AsQueryable();
var employeeAssignments = new List<EmployeeAssignment>
{
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 4), Project = projects.Single(x => x.Id == 1), StartDate = new DateTime(2016, 4, 10), EndDate = new DateTime(2016, 6, 20), CostOfJob = 35.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 2), Project = projects.Single(x => x.Id == 1), StartDate = new DateTime(2016, 4, 10), EndDate = new DateTime(2016, 5, 6), CostOfJob = 45.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 6), Project = projects.Single(x => x.Id == 2), StartDate = new DateTime(2016, 2, 23), EndDate = new DateTime(2016, 10, 1), CostOfJob = 77.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 2), Project = projects.Single(x => x.Id == 2), StartDate = new DateTime(2016, 5, 12), EndDate = new DateTime(2016, 5, 20), CostOfJob = 24.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 2), Project = projects.Single(x => x.Id == 2), StartDate = new DateTime(2016, 5, 23), EndDate = new DateTime(2016, 5, 30), CostOfJob = 60.5m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 5), Project = projects.Single(x => x.Id == 3), StartDate = new DateTime(2016, 9, 2), EndDate = new DateTime(2016, 11, 14), CostOfJob = 70.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 3), Project = projects.Single(x => x.Id == 3), StartDate = new DateTime(2016, 9, 2), EndDate = new DateTime(2016, 11, 14), CostOfJob = 85.0m },
new EmployeeAssignment { Employee = employees.Single( x => x.Id == 1), Project = projects.Single(x => x.Id == 3), StartDate = new DateTime(2016, 10, 3), EndDate = new DateTime(2016, 10, 20), CostOfJob = 80.0m }
}.AsQueryable();
var projectsSet = Substitute.For<DbSet<Project>, IQueryable<Project>>();
((IQueryable<Project>)projectsSet).Provider.Returns(projects.Provider);
((IQueryable<Project>)projectsSet).Expression.Returns(projects.Expression);
((IQueryable<Project>)projectsSet).ElementType.Returns(projects.ElementType);
((IQueryable<Project>)projectsSet).GetEnumerator().Returns(projects.GetEnumerator());
var employeeSet = Substitute.For<DbSet<Employee>, IQueryable<Employee>>();
((IQueryable<Employee>)employeeSet).Provider.Returns(employees.Provider);
((IQueryable<Employee>)employeeSet).Expression.Returns(employees.Expression);
((IQueryable<Employee>)employeeSet).ElementType.Returns(employees.ElementType);
((IQueryable<Employee>)employeeSet).GetEnumerator().Returns(employees.GetEnumerator());
var employeeAssignmentSet = Substitute.For<DbSet<EmployeeAssignment>, IQueryable<EmployeeAssignment>>();
((IQueryable<EmployeeAssignment>)employeeAssignmentSet).Provider.Returns(employeeAssignments.Provider);
((IQueryable<EmployeeAssignment>)employeeAssignmentSet).Expression.Returns(employeeAssignments.Expression);
((IQueryable<EmployeeAssignment>)employeeAssignmentSet).ElementType.Returns(employeeAssignments.ElementType);
((IQueryable<EmployeeAssignment>)employeeAssignmentSet).GetEnumerator().Returns(employeeAssignments.GetEnumerator());
_dbContext = Substitute.For<IProjectPersonsDbContext>();
_dbContext.Projects.Returns(projectsSet);
_dbContext.Employees.Returns(employeeSet);
_dbContext.EmployeeAssignments.Returns(employeeAssignmentSet);
}
[TestCaseSource(nameof(_projectCases))]
public void GetAvailableEmployeesForProject_Calculated(DateTime start, DateTime end, int[] expectedResult)
{
// Arrange
var dateHelper = Substitute.For<IDateHelper>();
var currencyHelper = Substitute.For<ICurrencyHelper>();
var employeeBusinessEngine = Substitute.ForPartsOf<EmployeeBusinessEngine>(_dbContext, dateHelper, currencyHelper);
// Act
var availableEmployees = employeeBusinessEngine.GetAvailableEmployeesForProject(start, end);
// Assert
CollectionAssert.AreEqual(expectedResult, availableEmployees.Select(e => e.Id).ToArray());
}
[TestCase(2, 2016, 5, ExpectedResult = 144)]
[TestCase(4, 2016, 5, ExpectedResult = 176)]
[TestCase(6, 2016, 2, ExpectedResult = 40)]
[TestCase(3, 2016, 9, ExpectedResult = 168)]
[TestCase(1, 2016, 10, ExpectedResult = 112)]
public int GetManHours_Calculated(int employeeId, int year, int month)
{
// Arrange
var dateHelper = new DateHelper();
var currencyHelper = Substitute.For<ICurrencyHelper>();
var employeeBusinessEngine = Substitute.ForPartsOf<EmployeeBusinessEngine>(_dbContext, dateHelper, currencyHelper);
// Act
int amountOfManHours = employeeBusinessEngine.GetManHours(employeeId, year, month);
return amountOfManHours;
}
[TestCaseSource(nameof(_incomeCases))]
public void GetMonthlyIncome_Calculated(int employeeId, int year, int month, decimal expectedIncome)
{
// Arrange
var dateHelper = new DateHelper();
var currencyHelper = Substitute.For<ICurrencyHelper>();
currencyHelper.GetExchangeRate(Arg.Any<DateTime>(), Arg.Is("GBP")).Returns(28.6187m);
var employeeBusinessEngine = Substitute.ForPartsOf<EmployeeBusinessEngine>(_dbContext, dateHelper, currencyHelper);
// Act
decimal actualIncome = employeeBusinessEngine.GetMonthlyIncome(employeeId, year, month);
// Assert
Assert.That(Math.Abs(expectedIncome - actualIncome), Is.LessThan(0.01m));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment