Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 httpcontext.current.server.mappath和单元测试_Asp.net Mvc 3_Unit Testing_Httprequest_Httpcontext - Fatal编程技术网

Asp.net mvc 3 httpcontext.current.server.mappath和单元测试

Asp.net mvc 3 httpcontext.current.server.mappath和单元测试,asp.net-mvc-3,unit-testing,httprequest,httpcontext,Asp.net Mvc 3,Unit Testing,Httprequest,Httpcontext,我将asp.net mvc3/razor与dbfirst模型一起使用。 我在注册后以xml格式发送确认电子邮件。 我需要在单元测试时读取这个xml文件, 由于httpcontext.current为null,我得到了null引用错误。我试图模拟它,但再次得到一个错误,说“值不能为null” 这是我的代码,请帮助: 会计控制员: Fds.ReadXml(HttpContext.Current.Server.MapPath("~/Files/ForgotPassword.xml")); 单元测

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

会计控制员:

  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()
        {

您需要抽象出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"));

您需要抽象出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"));