httpcontext.current.server.mappath和单元测试

时间:2013-08-19 08:49:11

标签: asp.net-mvc-3 unit-testing httprequest httpcontext

我在dbfirst模型中使用asp.net mvc3 / razor。 我在注册后发送一封xml格式的确认电子邮件。 我需要在单元测试时读取这个xml文件, 我得到null引用错误,因为httpcontext.current为null。我试图模仿它,但我又得到一个错误,说“值不能为null” 这是我的代码,请帮忙:

的AccountController:

  Fds.ReadXml(HttpContext.Current.Server.MapPath("~/Files/ForgotPassword.xml"));

单元测试:

 public void Saveuser()
         {
             RegisterModel model = new RegisterModel();
             FormCollection f = new FormCollection();
             List<MailModel> m = new List<MailModel>();
             HttpContext.Current = FakeHttpContext();
             m = user.GetMail().ToList();
             MailModel mmodel = new MailModel();
             mmodel = m[0];
           model.Name = "testuse11r9788";
          model.UserName = "test1user9878";
          model.Password = "1234567";
          model.ConfirmPassword = "1234567";
          model.Email = "testus11979@gmail.com";
          var controller = new AccountController(user,mail);
          var saveuser = controller.Register(model,f) as ViewResult;
          var saveActUser = (RegisterModel)saveuser.ViewData.Model;
          var saveExpUser = model;
           areuserEqual(saveExpUser, saveActUser);
         }

public static HttpContext FakeHttpContext()
        {

//请帮助我在httprequest ????

中输入的内容
  var httpRequest = new HttpRequest("", "http://localhost:mmmm/", "");
                 var stringWriter = new StringWriter();
                 var httpResponce = new HttpResponse(stringWriter);
                var httpContext = new HttpContext(httpRequest, httpResponce);

            var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
                                                    new HttpStaticObjectsCollection(), 10, true,
                                                    HttpCookieMode.AutoDetect,
                                                    SessionStateMode.InProc, false);

            httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
                                        BindingFlags.NonPublic | BindingFlags.Instance,
                                        null, CallingConventions.Standard,
                                        new[] { typeof(HttpSessionStateContainer) },
                                        null)
                                .Invoke(new object[] { sessionContainer });

            return httpContext;
        }

1 个答案:

答案 0 :(得分:3)

您需要抽象出XML文件的加载。

如;

class WebContentLocator : IContentLocator{
    public string GetPath(string relativePath) {
         return HttpContext.Current.Server.MapPath(relativePath);
    }
}

class TestContentLocator : IContentLocator{
    string _contentRoot;
    public TestContentLocator() {
         _contentRoot = ConfigurationManager.AppSettings["ContentRoot"];
    }

    public string GetPath(string relativePath) {
         return Path.Combine(_contentRoot,  relativePath.Replace("~", string.empty);
    }
}

interface IContentLocator {
    string GetPath(string relativePath);
}

并在您的测试中将TestContentLocator注入正在执行XML加载的代码中,默认情况下将使用WebContentLocator。

Fds.ReadXml(_contentLocator.Get("~/Files/ForgotPassword.xml"));