單元測試解決HttpContext的問題

C#代碼web

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Thread.GetDomain().SetData(".appPath", "c:\\inetpub\\wwwroot\\webapp\\");
            Thread.GetDomain().SetData(".appVPath", "/");
            TextWriter tw = new StringWriter();
            String address = "home.myspace.cn";
            HttpWorkerRequest wr = new MyWorkerRequest
            ("default.aspx", "friendId=1300000000", tw, address);
            HttpContext.Current = new HttpContext(wr);


            UsersController target = new UsersController(); // TODO: Initialize to an appropriate value
            JsonResult actual = target.login();
        }
    }

    public class MyWorkerRequest : SimpleWorkerRequest
    {
        private string localAdd = string.Empty;

        public MyWorkerRequest(string page, string query, TextWriter output, string address)
            : base(page, query, output)
        {
            this.localAdd = address;
        }

        public override string GetLocalAddress()
        {
            return this.localAdd;
        }
    }


版權聲明:本文爲博主原創文章,未經博主容許不得轉載。app

相關文章
相關標籤/搜索