c# - Testing Data Annotations using Moq in Entity Framework -


does moq take account data annotations while testing mock<dbcontext> , mock<dbset<ientity>>? instance, proper validation exceptions thrown if attempt expressly forbidden data annotations of code first entity model? if not, how can test expected results data annotations?

edit: should note using entity framework 6, has been overhauled work better mock frameworks.

the accepted wisdom in unit testing "don't test code don't own", in case if moq (and can't because mentioned ela, provides fake implementations of parts of interface) shouldn't - have accept dataannotations provided system.componentmodel (or whichever) have been tested author, , work advertised.

of course if have written own custom attribute unit test annotation validation code in separate test class tests functionality independent of being stacked onto property.

also, given have mock dbcontext , entityset, don't see dataannotations come - relevant in unit test implementation of actual entity, in case shouldn't anywhere near dbcontext or entityset - should manually creating entity (or mocking one) test @ hand. feel free let know context of these tests are!

update: in order have regression test presence of specific attribute on specific property, 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 out, explode test here.         }     } } 

i don't think there other reliable way enforce requirement, again may wrong...


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -