How do I mock/fake the session object in ASP.Net Web forms?

后端 未结 5 1068
终归单人心
终归单人心 2020-12-09 09:41

Is there a way to mock/fake the session object in ASP.Net Web forms when creating unit tests?

I am currently storing user details in a session variable which is acce

5条回答
  •  一整个雨季
    2020-12-09 10:40

    In Asp.Net webforms, you cannot escape the fact that framework's entry into your code comes from aspx pages. I agree that your business layer should not touch the asp.net specific components directly but you have to have a model's storage container and session in asp.net is a good area. Thus, one possible approach is to create ISessionManager for purpose of interacting inside your business layer. Then, implement the concrete type by using HttpSessionState ... btw, a good trick is to use HttpContext.Current.Session to implement accessors/getters out of the HttpSessionState. Your next challenge would be how to wire it all together.

提交回复
热议问题