asp.net mvc - Unit Testing on Controller that uses AutoMapper -


i trying unit test updateuser controller uses automapping. here code controller

updateusercontroller

    private readonly iunitofwork _unitofwork;     private readonly iwebsecurity _websecurity;     private readonly ioauthwebsecurity _oauthwebsecurity;     private readonly imapper _mapper;      public accountcontroller()     {         _unitofwork = new unitofwork();         _websecurity = new websecuritywrapper();         _oauthwebsecurity = new oauthwebsecuritywrapper();         _mapper = new mapperwrapper();     }      public accountcontroller(iunitofwork unitofwork, iwebsecurity websecurity, ioauthwebsecurity oauthwebsecurity, imapper mapper)     {         _unitofwork = unitofwork;         _websecurity = websecurity;         _oauthwebsecurity = oauthwebsecurity;         _mapper = mapper;     }      //     // post: /account/updateuser     [httppost]     [validateantiforgerytoken]     public actionresult updateuser(updateusermodel model)     {         if (modelstate.isvalid)         {             // attempt register user             try             {                 var usertoupdate = _unitofwork.userrepository.getbyid(_websecurity.currentuserid);                 var mappedmodel = _mapper.map(model, usertoupdate);   **mappedmodel return null when run in test fine otherwise (e.g. debug)**                   _unitofwork.userrepository.update(mappedmodel);                 _unitofwork.save();                  return redirecttoaction("index", "home");             }             catch (membershipcreateuserexception e)             {                 modelstate.addmodelerror("", errorcodetostring(e.statuscode));             }         }         return view(model);     } 

and unit test updateusercontrollertest

[fact]     public void userrepository_update_user_success()     {         controller = new accountcontroller(unitofwork, websecurity.object, oauthwebsecurity.object, mapper);         const string emailasusername = "user@username.com";         const string password = "password";         const string email = "email@email.com";         const string emailnew = "newemail@email.com";         const string firstname = "first name";         const string firstnamenew = "new first name";         const string lastname = "last name";         const string lastnamenew = "new last name";          var updateduser = new user             {                 email = emailnew,                 firstname = firstnamenew,                 lastname = lastnamenew,                 username = emailasusername             };          websecurity.setup(             s =>             s.createuserandaccount(emailasusername, password,                                    new { firstname = firstname, lastname = lastname, email = email }, false))                    .returns(emailasusername);         updateduser.userid = websecurity.object.currentuserid;          unitofwork.userrepository.update(updateduser);         unitofwork.save();          var actualuser = unitofwork.userrepository.getbyid(updateduser.userid);         assert.equal(updateduser, actualuser);          var model = new updateusermodel             {                 email = emailasusername,                 confirmemail = emailasusername,                 firstname = firstname,                 lastname = lastname             };         var result = controller.updateuser(model) redirecttorouteresult;         assert.notnull(result);     } 

i have gut feel when run in test mode, mapper not @ mapper configuration have setup in global.asax. since error occur during execution of unit test not when running website is. have created imappaer interface di can mock testing purposes. used moq mocking , xunit test framework, have installed automoq have not used yet. idea? thank looking @ lengthy post. hope can help, been scratching head hours , reading lots of posts.

in test need create mocked version of imapper interface otherwise aren't unit testing, you're integration testing. need simple mockmapper.setup(m => m.map(something, somethingelse)).returns(anotherthing).

if want use real automapper implementation in tests need set first. tests not automatically pick global.asax, you'll have set mappings in tests well. when i'm integration testing have static automapperconfiguration.configure() method call in test fixture setup. nunit [testfixturesetup] method, think xunit put in constructor.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -