Unit Testing Entity Framework with ms unit test framework -


i using ms unit test unit test project in i'm using entity framework. able mock using following example in msdn have scenario need use transactions in entity framework in following way

var tran = testdbcontext.database.begintransaction() 

and test fails database null testdbcontext.

i wondering if can mock somehow working

using linked msdn article reference notice have abstracted dbcontext. using same thinking should abstract creation of transaction.

first create abstraction of expected functionality transaction object.

/// <summary> /// wraps access transaction object on underlying store connection /// </summary> public interface idbcontexttransaction : idisposable {     /// <summary>     ///  commits underlying store transaction     /// </summary>     void commit();     /// <summary>     /// rolls underlying store transaction     /// </summary>     void rollback(); } 

this mirrors functionality want database transaction.

for production can have implementation wraps actual object want use.

public class dbcontexttransactionwrapper : idbcontexttransaction {     private dbcontexttransaction dbcontexttransaction;      public dbcontexttransactionwrapper(dbcontexttransaction dbcontexttransaction) {         this.dbcontexttransaction = dbcontexttransaction;     }      public void commit() {         dbcontexttransaction.commit();     }      public void rollback() {         dbcontexttransaction.rollback();     }      public void dispose() {         if(dbcontexttransaction != null) {             dbcontexttransaction.dispose();             dbcontexttransaction = null;         }     } } 

the dbcontext abstraction include ability create transactions...

public interface istoreappcontext : idisposable {     dbset<product> products { get; }     int savechanges();     void markasmodified(product item);     idbcontexttransaction begintransaction();   } 

and implementation use wrapper

public class storeappcontext : dbcontext, istoreappcontext {     public storeappcontext() : base("name=storeappcontext")     {     }      public dbset<product> products { get; set; }      public void markasmodified(product item)     {         entry(item).state = entitystate.modified;     }      public idbcontexttransaction begintransaction() {         return new dbcontexttransactionwrapper(database.begintransaction());     } } 

that way, in unit tests can mock creation of transaction via mocking framework or fake implementation , call directly on abstracted dbcontext.

assuming testdbcontext of type istoreappcontext call like...

idbcontexttransaction tran = testdbcontext.begintransaction(); 

this give access tran.commit() , tran.rollback() defined on interface , allow easy testing.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -