I have a working application for which I must now add a complete set of unit test. The current code stores user information in the following ways:
HttpContext.Current.Session ["UserInfo"] = userData;
I am using moq for my test, and my unit test has the following code:
var Server = new fake & lt; HttpServerUtilityBase & gt; (MockBehavior.Loose); Var response = new mask & lt; HTTPRPSPassbase & gt; (Fake Biievic. Secret); Var Requests = New Imitation & lt; HttpRequestBase & gt; (MockBehavior.Strict); Var reference = new fake & lt; HttpContextBase & gt; (); Context.SetupGet (x => x.Request). Return (request.Object); Reference.setupGet (x => x.Response). Response.Object; Reference.setupGet (x = & gt; x.Server). Return (server.Object); Var Controller = New Login Controller (); Controller Controller Resources = New Controlling (context, new RootData (), Controller); JsonResult result = Controller.login (new login inhale {user name = "myusername", password = "invalidpassword"}) as JSN;
Of course, when I try to create session process session data, I get an "object reference not set for an example of an object". Error because hpp The file is zero.
Some research has shown to me that using the current is not very compatible with MSTE, so I understand that the way I store / load my user information will need to be changed, however, I would like some advice on how to go here.
I would appreciate any suggestions on how to make tests for my unit, or to collect user information in a different way to make it compatible with unit tests.
This solution I came up with is a combination of some things that I have here in different places I've got:
First of all, I have created 2 squares. MockHttp session and MockHallers public class MockHttp session: HttpSessionStateBase {dictionary & lt; String, Object & gt; M_SessionStorage = New Dictionary & lt; String, Object & gt; (); Public override object this [string name] {return m_SessionStorage [name]; } Set {m_SessionStorage [name] = value; }} Public override zero Abandon () {// nothing}} Public class MockHelpers {public static HttpContext FakeHttpContext () {var httpRequest = new HttpRequest ("", "http: // localhost /", ""); Var stringWriter = new stringwriter (); Var httpResponse = New HttpResponse (stringWriter); Var httpContext = New HttpContext (httpRequest, httpResponse); Var sessionContainer = New HttpSessionStateContainer ("id", new session state compilation (), new HttpStaticObjectsCollection (), 10, true, HttpCookieMode.AutoDetect, sessionStateMode.InProc, incorrect); Session status AdhesHepiationStatisticsContact (HTQIntext, SessionContainer); Return httpContext; }}
Then I changed my unit test code to use them:
var server = new mask & lt; HTTP server utilitybase & gt; (Mokavihar, Loz); Var response = new mask & lt; HTTPRPSPassbase & gt; (Fake Biievic. Secret); Var Requests = New Imitation & lt; HttpRequestBase & gt; (MockBehavior.Strict); Var session = new MockHttp session (); Var reference = new fake & lt; HttpContextBase & gt; (); Context.SetupGet (x => x.Request). Return (request.Object); Reference.setupGet (x => x.Response). Response.Object; Reference.setupGet (x = & gt; x.Server). Return (server.Object); Context.SetupGet (x = & x; session.). Return (session); HttpContext.Current = MockHelpers.FakeHttpContext (); Var Controller = New Login Controller (); Controller Controller Resources = New Controlling (context, new RootData (), Controller); JsonResult result = Controller.login (New Login Heller {userName = "MyUserName", password = "" InvalidPassword}) as JsonResult;
This code was able to test successfully with both valid and invalid passwords.
Thanks to everyone for your help
No comments:
Post a Comment