locked
how to create mock or fake HttpContext.Current.Application for doing Nunit testing of controller RRS feed

  • Question

  • We have an already developed ASP.NET MVC application. We are told to write Unit test case using NUnit.

    This is how the application in developed.

    1) In the Session_Start in the Global.asax we hitting a database and fetching some lookup values from database and putting it in

    HttpContext.Current.Application["lookup"]=(dbvalues);

    2) In the ActionMethods of the controller and in some other classes validation methods we are fetching the values from the Application["lookup"] .

    we are assigning the Application["lookup"] to Viewbag and that will be used to list values in the view.

    For example : List of store we will put into Application["lookup"] and in the Index method of the controller we are assigning the ViewBag.Stores =Application["lookup"].

    Now we are told to write Nunit test cases for each and every controller methods.

    We created fake or mock HttpContext but the issue is with Httpcontext.current.Application.

    For example we created the Httpcontext.Current.Application["lookup"]="values" in the Nunit test methods but Application["lookup"] is no there when it hits the controller Index method. Please help how to create fake Httpcontext.current.Application["lookup"]

    Sunday, February 26, 2017 4:23 PM

All replies

  • Hi bSurendiran,

    According to your description, I think the following thread maybe helpful for you, please refer to original post and the answers from the following thread:

    http://stackoverflow.com/questions/35937377/mocking-httpcontext-current-application

    Sincerely,

    Oscar


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Oscar_Wu Monday, February 27, 2017 7:17 AM
    Monday, February 27, 2017 7:17 AM