简体   繁体   English

在实体框架中使用Moq测试数据注释

[英]Testing Data Annotations using Moq in Entity Framework

Does Moq properly take into account Data Annotations while testing a Mock<DbContext> and Mock<DbSet<IEntity>> ? Moq在测试Mock<DbContext>Mock<DbSet<IEntity>>是否正确考虑了数据注释? For instance, will the proper validation exceptions be thrown if I attempt to do something that is expressly forbidden by the Data Annotations of a Code First Entity Model? 例如,如果我尝试执行代码优先实体模型的数据注释明确禁止的操作,是否会抛出适当的验证异常? If not, how can I properly test my expected results from Data Annotations? 如果没有,如何正确测试数据注释的预期结果?

edit: I should note that I am using Entity Framework 6, as it has been overhauled to work much better with Mock Frameworks. 编辑:我应该注意,我使用的是Entity Framework 6,因为它已经过全面改进,可以更好地与Mock Frameworks协同工作。

The generally accepted wisdom in unit testing is "don't test code you don't own", so in this case even if Moq could do this (and it can't because as mentioned by Ela, it just provides fake implementations of certain parts of an interface) you shouldn't - you have to accept that the DataAnnotations provided by System.ComponentModel (or whichever) have been tested by their author, and work as advertised. 单元测试中普遍接受的智慧是“不要测试您不拥有的代码”,因此在这种情况下,即使Moq可以做到(而且不能这样做,因为正如Ela所提到的,它只是提供了某些实现的虚假实现)接口的一部分),您不应该-必须接受System.ComponentModel (或任何一种)提供的DataAnnotations已经过其作者的测试,并能按广告规定工作。

Of course if you have written your own custom attribute then you unit test this annotation validation code in a separate test class that tests its functionality independent of it being stacked onto a property. 当然,如果您编写了自己的自定义属性,则可以在一个单独的测试类中对该注释验证代码进行单元测试,该类独立于将其堆叠到属性上来对其功能进行测试。

Also, given that you have a Mock DbContext and EntitySet , I don't even see where DataAnnotations come into it - they would only be relevant in a unit test for some implementation of an actual entity , in which case you shouldn't be anywhere near a DbContext or EntitySet - you should be manually creating an entity (or mocking one) for the test at hand. 另外,假设您具有Mock DbContextEntitySet ,我什至看不到DataAnnotations进入的位置-它们仅在单元测试中与实际实体的某些实现相关,在这种情况下,您不应该在任何地方在DbContextEntitySet附近-您应该手动为即将进行的测试创建一个实体(或DbContext一个实体)。 Feel free to let us know what the context of these tests are! 随时让我们知道这些测试的内容是什么!

Update: In order to have a regression test for the presence of a specific attribute on a specific property, you could use reflection: 更新:为了对特定属性上特定属性的存在进行回归测试,可以使用反射:

public void MyEntityClass_PropertyFoo_HasRequiredAttribute()
{
    var prop = typeof(MyEntity).GetProperties().FirstOrDefault(p=>p.Name=="Foo");
    if (prop!=null)
    {
        object[] attributes = prop.GetCustomAttributes(typeof(RequiredAttribute), true);
        if (attributes.Length==0)
        {
           //someone took it out, explode your test here.
        }
    }
}

I don't think there is any other reliable way to enforce that requirement, but then again I may well be wrong... 我认为没有其他可靠的方法可以执行该要求,但是我可能还是错了。

Mock only gives you a "faked" object, it does not implement any functionality. 模拟仅给您一个“伪造的”对象,它没有实现任何功能。 It is just to control parts of your code or prevent null pointer exception because certain instances are not set up at all. 这只是为了控制代码的一部分或防止空指针异常,因为根本没有设置某些实例。 But all methods which are not specifically set up within a mocked object will not do anything. 但是,不是在模拟对象中专门设置的所有方法都不会做任何事情。

There are certain articles of how to unit test entity framework, maybe this helps. 关于如何对实体框架进行单元测试的某些文章,也许会有所帮助。 For example: http://msdn.microsoft.com/en-us/ff714955.aspx 例如: http : //msdn.microsoft.com/en-us/ff714955.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM