unit testing - How to mock Asp.net identity UserManager to list user of Specific role -


i trying write unit test of index method of clientcontroller.

here clientcontroller:

   public clientcontroller(applicationusermanager clientmanager, applicationrolemanager rolemanager)     {         clientmanager = clientmanager;         rolemanager = rolemanager;     }      private applicationrolemanager _rolemanager;     public applicationrolemanager rolemanager     {                 {             return _rolemanager ?? httpcontext.getowincontext().get<applicationrolemanager>();         }         set         {             _rolemanager = value;         }     }      private applicationusermanager _clientmanager;     public applicationusermanager clientmanager     {                 {             return _clientmanager ?? httpcontext.getowincontext().getusermanager<applicationusermanager>();         }         set         {             _clientmanager = value;         }     }      public async task<actionresult> index(string filter, string error, string searchname, int? page, int? records)     {         list<string> filtercriteria = new list<string>();         filtercriteria.add("choose email");         var listclients = new list<applicationuser>();         // list of clients ( users client role )         foreach (var user in clientmanager.users.where(u => u.isactive == true && (u.firstnames.contains(searchname) || u.lastname.contains(searchname)         || searchname == null)).orderby(u => u.firstnames).tolist())         {             if (await clientmanager.isinroleasync(user.id, "client"))             {                 listclients.add(user);                 filtercriteria.add(user.email);             }         }         viewbag.emails = new selectlist(filtercriteria);         viewbag.error = error;          if (filter == null || filter.equals("choose email"))         {             return view(listclients.tolist().topagedlist(page ?? 1, records ?? 15));         }         else         {             return view();         } 

and here attempt write unit test of it.

 [testmethod]     public void index_get_retrievesallclientfromrepository()     {         // arrange,         applicationuser client1 = getclientnamed("1", 1, 1, datetime.now, "abc", "abc", "xyz", "343433443", "abc@xyz.com", "m", "06091980-absd");         var userstore = new mock<iuserstore<applicationuser>>();          var usermanager = new usermanager<applicationuser>(userstore.object);         userstore.setup(x => x.createasync(client1))             .returns(task.fromresult(identityresult.success));          userstore.setup(x => x.findbynameasync(client1.username))                     .returns(task.fromresult(client1));          var rolestore = new mock<irolestore<identityrole>>();         var rolemanager = new mock<applicationrolemanager>(rolestore.object);          var controller = new clientcontroller(             userstore.object applicationusermanager, rolemanager.object);         // act         var resulttask = controller.index("choose email", "", "", 1, 15);         resulttask.wait();         var result = resulttask.result;         var model = (list<applicationuser>)((viewresult)result).model;         collectionassert.contains(model, client1);     } 

userstore.object come null. quite newbie in unit testing , have looked many solution there isn't such use case. appreciated.

userstore.object coming null because of as cast, returns null if conversion isn't possible.

userstore defined as:

new mock<iuserstore<applicationuser>>();

which means .object of type iuserstore<applicationuser>,

which isn't applicationusermanager, end null.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -