0
単体テストで何も返されず、メソッド内のグループまたは計算によってリストが0カウントのまま残っていれば不思議でした。適切な方向への助けや指摘があれば幸いです。ありがとうございました!DateTimeの差の計算グループ内の合計Linqとエンティティ
ユニットテスト:
#region CalculateTimeToPay_IntegerEmployeeIDAndDateTimeWeekOfDate_IQueryableTimeToPay
[TestMethod]
public void CalculateTimeToPay_IntegerEmployeeIDAndDateTimeWeekOfDate_IQueryableTimeToPay()
{
//Arrange
var service = new WWIncomeTaxDataHandlerService("ProdSQL");
DateTime payrollWeekEnd = Convert.ToDateTime("2017-01-22");
DateTime payrollWeekStart = Convert.ToDateTime("2017-01-16");
var V_Times = new List<V_Time>()
{
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-05 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-05 16:00:00.000"), EmployeeID = 999 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-05 08:30:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-05 16:00:00.000"), EmployeeID = 777 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-05 08:45:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-05 16:00:00.000"), EmployeeID = 888 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-10 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-10 16:00:00.000"), EmployeeID = 999 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-10 08:30:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-10 16:00:00.000"), EmployeeID = 777 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-10 08:45:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-10 16:00:00.000"), EmployeeID = 888 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-11 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-11 16:00:00.000"), EmployeeID = 999 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-11 00:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-11 07:00:00.000"), EmployeeID = 777 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-11 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-11 16:00:00.000"), EmployeeID = 888 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-12 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-12 16:00:00.000"), EmployeeID = 999 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-12 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-12 16:44:00.000"), EmployeeID = 777 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-12 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-12 16:00:00.000"), EmployeeID = 888 },
new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-14 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-14 16:07:00.000"), EmployeeID = 777 }
};
var mockRepository = new Mock<IRepository>();
mockRepository.Setup(x => x.Find<V_Time>()).Returns(V_Times.AsQueryable());
var builder = BuildContainer();
builder.Register(x => mockRepository.Object).As<IRepository>();
var container = builder.Build();
var itrs = container.Resolve<WWIncomeTaxDataHandler.Domain.WWIncomeTaxDataHandlerService>();
var expected = new List<calculatedHours>()
{
new calculatedHours { EmployeeID = 999, TimeToPay = 24 },
new calculatedHours { EmployeeID = 888, TimeToPay = 31.35},
new calculatedHours { EmployeeID = 777, TimeToPay = 23.25}
};
//Act
var actual = itrs.CalculateTimeToPay(payrollWeekStart, payrollWeekEnd);
//Assert
var compareLogic = new CompareLogic();
var result = compareLogic.Compare(actual, expected);
Assert.IsTrue(result.AreEqual, result.DifferencesString);
}
#endregion
方法:バック照会可能にToListメソッドとは、より高速なテストのためだった
#region CalculateTimeToPay(payrollWeekEnd)
public IQueryable CalculateTimeToPay(DateTime payrollWeekStart, DateTime payrollWeekEnd)
{
var uow = container.Resolve<WWIncomeTaxDataHandlerUnitOfWork>();
var employeeHours = (from time in uow.Accounting.Repository.Find<V_Time>()
where (payrollWeekEnd >= time.PayTimeIn && time.PayTimeIn >= payrollWeekStart)
group time by new { time.EmployeeID } into empGroup
select new calculatedHours
{
EmployeeID = empGroup.Key.EmployeeID,
TimeToPay = empGroup.Where(x => x.EmployeeID == empGroup.Key.EmployeeID).Sum (x => ((x.PayTimeOut - x.PayTimeIn).TotalSeconds)/60/60)
}).ToList();
return employeeHours.AsQueryable();
}
#endregion
ノート
私は今や馬鹿です。それは、私が使用していた時間枠であり、予想通りに変更した後にオフになっていたことを認識していませんでした。お手伝いありがとう :) –