asp.net mvc - Unit testing sessions is still returning null after Mocking -


i trying write unit test test controller makes use of session. understand concept httpcontext generated request browser , since calling controller action form unit test, current context null.

i tried solve issue based on first part of article http://www.dontpaniclabs.com/blog/post/2011/03/22/testing-session-in-mvc-in-four-lines-of-code/ still isn't working me. see code below.

    [testfixturesetup]     public void configure_automapper()     {         _product = new productviewmodel()         {             id = guid.newguid(),             category = guid.parse("72e42a11-dfa4-4ab9-a735-5cd5c18396d4"),             brand = guid.parse("96e68b67-60fb-4531-898e-815433b44d89"),             description = "from test",             costprice = parse("17.00"),             saleprice = parse("25.00"),             quantity = 7,             businessid = guid.parse("bdf9fb7f-db40-4365-9872-aecd616ed415")         };          var context = new mock<controllercontext>();         var session = new mock<httpsessionstatebase>();         context.setup(m => m.httpcontext.session).returns(session.object);         _productscontroller = new productscontroller {controllercontext = context.object};          var usersessioninfo = new usersessionviewmodel()         {             firstname = "junior",             lastname = "labolz",             businessid = guid.parse("bdf9fb7f-db40-4365-9872-aecd616ed415")         };          sessionhelper.adduserdetailstosession(sessionhelper.userdetails, usersessioninfo);     }       [test]     public void display_products_list_on_view_products_button_click()     {         //set          //act         var viewresult = (viewresult)_productscontroller.index();          //assert         assert.that(string.empty, is.equalto(viewresult.viewname));     }       public static void adduserdetailstosession(string sessionname, usersessionviewmodel usersessiondetails)     {         httpcontext.current.session.add(sessionname, usersessiondetails);     } 

**based on richard's answer changed **

        var context = new mock<controllercontext>();         var session = new mock<httpsessionstatebase>();         context.setup(m => m.httpcontext.session).returns(session.object);         var usersessioninfo = new usersessionviewmodel()         {             firstname = "junior",             lastname = "labolz",             businessid = guid.parse("bdf9fb7f-db40-4365-9872-aecd616ed415")         };         context.object.httpcontext.adduserdetailstosession(sessionhelper.userdetails, usersessioninfo);          _productscontroller = new productscontroller {controllercontext = context.object}; 

product controller constructor

    public productscontroller()     {         _productservice = new productserviceclient();         _usersessionviewmodel = (usersessionviewmodel)sessionhelper.getsessiondetail(sessionhelper.userdetails);     } 

get session method

    public static object getsessiondetail(string sessionname)     {         return httpcontext.current.session[sessionname];     } 

i can see code contains static function references httpcontext.current directly. cannot if want unit test, must work injected instances of httpcontextbase instead.

in unit tests, these instances mock context. in production, set container inject httpcontext.current when httpcontextbase requested.

i rewrite static method extension method:

public static void adduserdetailstosession(this httpcontextbase context,      string sessionname, usersessionviewmodel usersessiondetails) {     context.session.add(sessionname, usersessiondetails); } 

then call test:

context.object.adduserdetailstosession(sessionhelper.userdetails, usersessioninfo); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -