c# - How to test MVC actions with Code First? -


i'm trying test mvc actions, created irepository , mockrepository

public class repository : dbcontext, irepository {     public idbset<tentity> someentities { get; set; } }  public interface irepository : idisposable {     idbset<tentity> someentities { get; set; }      int savechanges(); } 

with create , delete actions simple, stuck edit action :

    private irepository repository;      public actionresult edit(tentity entity)     {         if (modelstate.isvalid)         {             repository.entry(entity).state = entitystate.modified;             repository.savechanges();              return redirecttoaction("index");         }          return view(entity);     } 

so see 2 ways solve problem:

  1. should add irepository new method

    dbentityentry<tentity> entry<tentity>(tentity entity) tentity : class; 

    how this? dbcontext.entry method returns specific dbentityentry<tentity> type?

  2. or change way update entity? recommended way this?

i abstract functionality of ef more are, meaning actions following.

private irepository repository; public actionresult edit(tentity entity) {     if (modelstate.isvalid)     {         repository.update(entity);         repository.savechanges();          return redirecttoaction("index");     }      return view(entity); } 

then can make mock repository , test desired functions called.

note: separate entities models , manage unit of work using action filter, that's not related post.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -