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