0

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

账户控制人:

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

1 回答 1

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"));
于 2013-11-15T11:15:45.613 回答