代码改变世界

单元测试解决HttpContext的问题

2015-06-05 21:44  -夜  阅读(405)  评论(0编辑  收藏  举报

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

作者:夜 本文地址:http://www.cnblogs.com/ful1021 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。如有问题,可以邮件:531761819@qq.com 联系我,非常感谢。