- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我目前正在尝试对通过 Entity Framework 运行的查询运行一些单元测试。查询本身在实时版本上运行没有任何问题,但单元测试总是失败。
我已将此范围缩小到我对 DbFunctions.TruncateTime 的使用,但我不知道有什么方法可以让单元测试反射(reflect)实时服务器上发生的情况。
这是我正在使用的方法:
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);
}
如果我注释掉包含 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, false) // Create a user placement that is not current
}.AsQueryable();
var set = DLTestHelper.GetMockSet<par_UserPlacement>(up);
var context = DLTestHelper.Context;
context.Setup(c => c.par_UserPlacement).Returns(set.Object);
var getter = DLTestHelper.New<LinqUserGetLinkedUsersForParentUser>(context.Object);
var output = getter.GetLinkedUsers(1);
var users = new List<User>();
output.ProcessDataTable((DataRow row) => students.Add(new UserStudent(row)));
Assert.AreEqual(1, users.Count);
Assert.AreEqual(2, users[0].UserId);
}
编辑 2:这是相关测试的消息和调试跟踪:
Test Result: Failed
Message: Assert.AreEqual failed. Expected:<1>. Actual:<0>
Debug Trace: This function can only be invoked from LINQ to Entities
从我读到的内容来看,这是因为没有 LINQ to Entities 实现此方法可以在这个地方用于单元测试,尽管在实时版本上有(因为它正在查询 SQL服务器)。
最佳答案
我知道我来晚了,但一个非常简单的解决方法是编写您自己的使用 DbFunction 属性的方法。然后使用该函数代替 DbFunctions.TruncateTime。
[DbFunction("Edm", "TruncateTime")]
public static DateTime? TruncateTime(DateTime? dateValue)
{
return dateValue?.Date;
}
使用此函数将在 Linq to Entities 使用时执行 EDM TruncateTime 方法,否则将运行提供的代码。
关于c# - 如何在单元测试中使用 Moq 和 DbFunctions 来防止 NotSupportedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21407748/
我有一个测试,我像这样传入一个对象: var repo = new ActualRepo(); var sut = new Sut(repo); 在我的测试中,Repo 有一个我需要实际执行的方法,而
想知道是否有可能将 Prism 事件聚合器最小化 让我们以他们拥有的EventAggregator快速入门为例 [TestMethod] public void Presente
我正在 mock MSMQ 的包装器。包装器只是允许创建一个直接调用 MessageQueue 类的静态方法的对象实例。 我想测试阅读队列是否筋疲力尽。为此,我希望模拟包装器返回一些好的结果,并在第四
我正在尝试开始使用最小起订量,但无法找到任何好的资源来做我需要的事情。 我有一个数据接口(interface)类,它有一个通过存储过程返回数据集的 Get 方法。这就是代码的编写方式,我目前无法更改它
我想使用 Moq 框架。我想下载框架,所以我到达了http://code.google.com/p/moq/通过谷歌,但第一行提到这个项目已转移到GitHub。当我去 GitHub 时,我只看到了源代
我想看看 Moq 是否是我想在新项目中使用的东西,因为我使用的其他模拟框架正在挑战恕我直言。例如,我有一个方法: IEnumerable GetPickLists(); 我不确定我应该如何 mock
这看起来很简单,但我似乎无法让它工作。 我有一个带有 Save 方法的类,它只调用另一个方法 ShouldBeCalled()。我想验证如果我调用 Save() 另一个方法 ShouldBeCalle
我正在修改一个类方法,它格式化一些输入参数日期,这些日期随后在方法调用中用作参数到基类(位于另一个程序集中)。 我想验证我传递给我的方法的日期在传递给基类方法时的格式是否正确,因此我想对基类方法调用进
我有以下方法: public CustomObect MyMethod() { var lUser = GetCurrentUser(); if (lUser.HaveAccess)
我有一个对象,我正在尝试使用最小起订量来模拟。该对象的构造函数具有必需的参数: public class CustomerSyncEngine { public CustomerSyncEng
我找不到对齐的 moq 和 moq contrib 版本。我可能只是谷歌失败了。 详情: 是否有适用于 moq 版本 4.0.10827 的 moq.contrib 版本,最新版本来自 http://
有没有人试过这个? 我喜欢最小起订量,我喜欢 pex 正在做的事情,但还没有一起尝试过。我认为在大多数情况下,我更喜欢使用 moq 而不是痣,但很想知道是否有人遇到了障碍? 他们玩得好吗? 最佳答案
VerABLE() 的目的是什么? 如果我验证 Mock 并将其保留,它仍然会验证 SetUp。 编辑:我使用的是VerifyAll(),因此所有内容都被验证。更改为 Verify() 后,仅检查我的
我在一个使用 Moq 和 AutoFixture.AutoMoq 的特定项目的构建服务器上运行单元测试时遇到问题。这是错误: System.IO.FileLoadException : Could n
我正在尝试创建一个具体类的模拟,并用另一个模拟来模拟它的一个属性。 public class MyClass { public virtual IAdapter Adapter {get; int
我无法使用 Moq (v.4.2) 在 protected 方法上设置回调。 代码如下所示: public abstract class AbstractClass { protected a
我在我的测试约定中使用 AutoMoqCustomization。 考虑下面的代码。一切都很好,直到我向其中一个具体类添加构造函数。当我这样做时,我得到“找不到无参数构造函数”。我们知道 AutoFi
在 Moq 中模拟 protected 虚拟(非通用)方法很容易: public class MyClass { .... protected virtual int MyMethod(D
如何验证是否使用 Moq 调用了“CallWithRef”方法? public interface ITest { void CallWithoutRef(string value, List
在我的单元测试中使用 Moq 生成 stub 和模拟,我有一个案例,我想验证是否调用了采用 Delegate 参数的方法。我不关心提供的特定 Delegate 参数,我只想确保该方法实际上被调用。该方
我是一名优秀的程序员,十分优秀!