【发布时间】:2014-04-24 19:09:42
【问题描述】:
如何模拟这个服务中的httpcontext?
当我尝试对该服务进行单元测试时,它抱怨 httpcontext 为空。
什么可以用来代替httpcontext?我使用的是网络表单而不是 mvc。我看过多篇关于伪造 httpcontext 类但不使用 webforms 的帖子。
public class FileService : IFileService
{
public string GetEmployeePicLocation(Employee employee)
{
string AgentFilesDirectory = "~\\AgentFiles\\";
Image newImage = new Image();
DirectoryInfo diSubPath =
new DirectoryInfo(HttpContext.Current.Server
.MapPath(AgentFilesDirectory + employee.User_Name));
string localDIR = AgentFilesDirectory + employee.User_Name + "\\";
string defaultDIR = AgentFilesDirectory + "nopic\\nopic.jpg";
string strFile;
if (diSubPath.Exists)
{
FileInfo[] arrJPG = diSubPath.GetFiles("*.jpg");
FileInfo[] arrPNG = diSubPath.GetFiles("*.png");
if (!(arrJPG.Length == 0) || !(arrPNG.Length == 0))
{
foreach (FileInfo f in diSubPath.GetFiles("*.jpg"))
{
newImage.ImageUrl = localDIR + employee.PicFile;
}
foreach (FileInfo f in diSubPath.GetFiles("*.png"))
{
newImage.ImageUrl = localDIR + employee.PicFile;
}
}
else
{
newImage.ImageUrl = defaultDIR;
}
}
if (!(diSubPath.Exists))
{
newImage.ImageUrl = defaultDIR;
}
return newImage.ImageUrl.ToString();
}
}
【问题讨论】:
标签: unit-testing integration-testing moq service-layer