httpcontext in asp.net unit test

 

        [TestMethod]
        [HostType("ASP.NET")]
        [UrlToTest("http://localhost:25153/qq/a.aspx")]
        [AspNetDevelopmentServerHost(@"C:\Users\Public\Documents\ooooooo", "/")]
        public void Test1()
        {
            var url = HttpContext.Current.Request.Url;
            var root = HttpContext.Current.Server.MapPath("~/");
            Assert.AreNotEqual(url, null);
        }

Using above code, there is a probem that the unit test method can‘t be debug, the break point will never be hitted.

 

 

        [TestMethod]
        [HostType("ASP.NET")]
        [UrlToTest("http://localhost:25153/qq/a.aspx")]
        [AspNetDevelopmentServerHost(@"C:\Users\Public\Documents\ooooooo", "/")]
        public void Test1()
        {

            //System.Diagnostics.Debugger.Break();
            System.Threading.Thread.Sleep(10 * 1000);
//attach the web server process when it launched
            var url = HttpContext.Current.Request.Url;
            var root = HttpContext.Current.Server.MapPath("~/");
            Assert.AreNotEqual(url, null);
        }

 

 

 

 

 

 reference:

http://msdn.microsoft.com/en-us/library/ms243172.aspx

http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。