我目前正在尝试对通过实体框架运行的查询运行一些单元测试.查询本身在实时版本上运行没有任何问题,但单元测试总是失败.
我把它缩小到我对DbFunctions.TruncateTime的使用,但我不知道如何通过这种方式来获得单元测试以反映实时服务器上发生的情况.
这是我正在使用的方法:
public System.Data.DataTable GetLinkedUsers(int parentUserId)
{
var today = DateTime.Now.Date;
var query = from up in DB.par_UserPlacement
where up.MentorId == mentorUserId
&& DbFunctions.TruncateTime(today) >= DbFunctions.TruncateTime(up.StartDate)
&& DbFunctions.TruncateTime(today) <= DbFunctions.TruncateTime(up.EndDate)
select new
{
up.UserPlacementId,
up.Users.UserId,
up.Users.FirstName,
up.Users.LastName,
up.Placements.PlacementId,
up.Placements.PlacementName,
up.StartDate,
up.EndDate,
};
query = query.OrderBy(up => up.EndDate);
return this.RunQueryToDataTable(query);
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉带有DbFunctions的行,则测试全部通过(除了检查只运行给定日期的有效结果的那些).
有没有办法可以提供在这些测试中使用的DbFunctions.TruncateTime的模拟版本?本质上它应该只返回Datetime.Date,但在EF查询中不可用.
编辑:这是使用日期检查失败的测试:
[TestMethod]
public void CanOnlyGetCurrentLinkedUsers()
{
var up = new List<par_UserPlacement>
{
this.UserPlacementFactory(1, 2, 1), // Create a user placement that is current
this.UserPlacementFactory(1, 3, 2, …Run Code Online (Sandbox Code Playgroud) 我一直收到上面的错误消息,即使我注释掉了发生错误的行.知道是什么原因引起的吗?我已经尝试用测试值重写行,但我仍然得到相同的错误.
这在调试模式下完美运行,只有在部署中才会出现这种情况.
原始代码:
Line 21: string domain, username;
Line 22: string text = Page.User.Identity.Name;
Line 23:
Line 24: domain = text.Substring(0, text.IndexOf("\\"));
Line 25: username = text.Substring(text.IndexOf("\\") + 1, text.Length - text.IndexOf("\\") - 1);
Source File: F:\<file path>\Default.aspx.cs Line: 23
Run Code Online (Sandbox Code Playgroud)
测试代码(相同的错误):
Line 21: string domain, username;
Line 22: //string text = "TEST"; // Page.User.Identity.Name;
Line 23: // this line is blank
Line 24: domain = "TEST"; //text.Substring(0, text.IndexOf("\\"));
Line 25: username = "TEST"; // text.Substring(text.IndexOf("\\") + 1,
Source File: F:\<file …Run Code Online (Sandbox Code Playgroud) 首先,我是LINQ的新手,所以我真的不知道它的来龙去脉.我试着在一些代码中使用它,根据我的诊断,它似乎与以相同方式使用for循环一样快.但是,我不确定这会如何扩展,因为我正在使用的列表可能会非常显着地增加.
我使用LINQ的碰撞检测功能的一部分(这仍然是在作品),我用它来剔除名单仅是相关的检查的.
这是LINQ版本:
partial class Actor {
public virtual bool checkActorsForCollision(Vector2 toCheck) {
Vector2 floored=new Vector2((int)toCheck.X, (int)toCheck.Y);
if(!causingCollision) // skip if this actor doesn't collide
return false;
foreach(
Actor actor in
from a in GamePlay.actors
where a.causingCollision==true&&a.isAlive
select a
)
if( // ignore offscreen collisions, we don't care about them
(actor.location.X>GamePlay.onScreenMinimum.X)
&&
(actor.location.Y>GamePlay.onScreenMinimum.Y)
&&
(actor.location.X<GamePlay.onScreenMaximum.X)
&&
(actor.location.Y<GamePlay.onScreenMaximum.Y)
)
if(actor!=this) { // ignore collisions with self
Vector2 actorfloor=new Vector2((int)actor.location.X, (int)actor.location.Y);
if((floored.X==actorfloor.X)&&(floored.Y==actorfloor.Y))
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我以前的方法:
partial class …Run Code Online (Sandbox Code Playgroud)