单元测试解决HttpContext的问题

C#代码

    [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;
        }
    }


你可能感兴趣的:(asp.net)