c# - Unit testing Entity Framework 6 with context inheriting from IdentityDbContext -


i'm looking unit testing methods using entity framework 6 using instructions provided here.

my set-up different though - i'm using asp.net identity (the default implementation uses ef). such context inherits identitydbcontext.

when run tests exception following details:

castle.proxies.identityuserlogin: : entitytype 'identityuserlogin' has no key defined. define key entitytype.     castle.proxies.identityuserrole: : entitytype 'identityuserrole' has no key defined. define key entitytype.     identityuserlogins: entitytype: entityset 'identityuserlogins' based on type 'identityuserlogin' has no keys defined.     identityuserroles: entitytype: entityset 'identityuserroles' based on type 'identityuserrole' has no keys defined. 

i've read in normal uses these set in default onmodelcreating method.

but can offer advice on handling within mocked context illustrated in method linked above?

thanks

andy

as pointed out in article, when mock out ef types change behaviour: queries executed linq-to-objects, not linq-to-sql. means can write unit tests pass code fails when used in real world. example is:

context.foos.where(candidate => somepredicate(candidate)); 

that means need understand valid linq-to-sql expressions write unit tests, writing unit tests because wanted validate understanding. has bought you? how value have got unit tests? i'm saying perspective of once spent ages mocking out ef layer find once database got involved code wrong.

a better testing strategy in opinion create gateway classes act intermediary between dbcontext , consumers of context.

class foogateway : ifoogateway {     ienumerable<foo> getfoosthatobeysomepredicate() { /* ... */ } } 

the gateways expose interfaces can mocked. use interfaces in data consuming classes can write high quality unit tests , accept need write integration tests test gateway classes themselves: , tests need real database, need infrastructure creating databases part of testing.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -