【发布时间】:2017-01-26 15:59:40
【问题描述】:
我正在为我的方法创建单元测试
[Authorize]
[HttpPost]
public async Task<JsonResult> UpdateDisplayName(string displayname)
{
bool status = _myProfileService.UpdateDisplayName(displayname, SessionAdapter.Instance._userDetailsViewModel.id);
if (status)
SessionAdapter.Instance._userDetailsViewModel.display_name = displayname;
return Json(new { status = status, displayname = displayname }, JsonRequestBehavior.AllowGet);
}
我的测试方法是
[TestMethod]
public async Task UpdateDisplayName_Test()
{
//Arrange
var controller = new HomeController(userServiceMock.Object, myProfileServiceMock.Object);
string displayName = "display";
const string expected = "{ status = False, displayname = display }";
myProfileServiceMock.Setup(m => m.UpdateDisplayName(It.IsAny<string>(), 1)).Returns(false);
//var controllerContextMock = new Mock<ControllerContext>();
//Act
var result = await controller.UpdateDisplayName(displayName) as JsonResult;
//Assert
Assert.AreEqual(expected, result.Data.ToString());
}
我的会话信息类在下面,这个类我在会话适配器中使用
public class SessionInfo
{
public string Id { set; get; }
public string Email { set; get; }
public string UserName { set; get; }
public UserDetailsViewModel _userDetailsViewModel { set; get; }
public string permission { set; get; }
//public string organization { set; get; }
public OrganizationViewModels Organization { set; get; }
public List<UserTeamModels> teams { set; get; }
public string status { set; get; }
public string role { set; get; }
public List<string> roles { set; get; }
}
我无法实例化SessionAdapter。如何对此进行单元测试?
我的 Interface 和 sessionadapter 类如下所示
public interface ISessionAdapterService
{
void Clear();
void Abandon();
bool DoesSessionExists { get; }
SessionInfo Instance { get; set; }
}
public class SessionAdapterService : ISessionAdapterService
{
private string sessionKey = "sessionInfoKey";
public bool DoesSessionExists
{
get
{
return HttpContext.Current.Session[sessionKey] == null ? false : true;
}
}
public SessionInfo Instance
{
get
{
return HttpContext.Current.Session[sessionKey] == null ? null : (SessionInfo)HttpContext.Current.Session[sessionKey];
}
set
{
HttpContext.Current.Session[sessionKey] = value;
}
}
public void Abandon()
{
HttpContext.Current.Session.Abandon();
HttpContext.Current.Session[sessionKey] = null;
}
public void Clear()
{
HttpContext.Current.Session.Clear();
}
}
我的测试用例和下面的答案一样
[TestMethod]
public async Task UpdateDisplayName_Test() {
//Arrange
var mySessionAdaptorService = new Mock<ISessionAdaptorService>();
var controller = new HomeController(userServiceMock.Object, myProfileServiceMock.Object, mySessionAdaptorService.Object);
var displayName = "display";
var status = false;
var id = 1;
myProfileServiceMock.Setup(m => m.UpdateDisplayName(It.IsAny<string>(), id)).Returns(status);
mySessionAdaptorService.Setup(m => m.Instance.Id).Returns(id);
//Act
var result = await controller.UpdateDisplayName(displayName) as JsonResult;
//Assert
Assert.IsNotNull(result);
}
代码更新。请找到我用于 SessionAdapter 类和 ISessionAdapter 接口以及实现的以下代码。请给出您的建议是正确的方法。
public interface ISessionInfo
{
string Id { set; get; }
string Email { set; get; }
string UserName { set; get; }
UserDetailsViewModel _userDetailsViewModel { set; get; }
string permission { set; get; }
OrganizationViewModels Organization { set; get; }
List<UserTeamModels> teams { set; get; }
string status { set; get; }
string role { set; get; }
List<string> roles { set; get; }
}
public class SessionInfo : ISessionInfo
{
public string Id { set; get; }
public string Email { set; get; }
public string UserName { set; get; }
public UserDetailsViewModel _userDetailsViewModel { set; get; }
public string permission { set; get; }
//public string organization { set; get; }
public OrganizationViewModels Organization { set; get; }
public List<UserTeamModels> teams { set; get; }
public string status { set; get; }
public string role { set; get; }
public List<string> roles { set; get; }
}
public interface ISessionAdapter
{
void Clear();
void Abandon();
bool DoesSessionExists { get; }
ISessionInfo Instance { get; set; }
}
public class SessionAdapter : ISessionAdapter
{
private string sessionKey = "sessionInfoKey";
public bool DoesSessionExists
{
get
{
return HttpContext.Current.Session[sessionKey] == null ? false : true;
}
}
public ISessionInfo Instance
{
get
{
return HttpContext.Current.Session[sessionKey] == null ? null : (SessionInfo)HttpContext.Current.Session[sessionKey];
}
set
{
HttpContext.Current.Session[sessionKey] = value;
}
}
public void Abandon()
{
HttpContext.Current.Session.Abandon();
HttpContext.Current.Session[sessionKey] = null;
}
public void Clear()
{
HttpContext.Current.Session.Clear();
}
【问题讨论】:
-
什么是 SessionAdapter?
-
会话适配器是我的静态类,具有一些属性,我在登录时设置这些详细信息
-
你不能实例化一个静态类。这就是静态的含义。
-
您遇到错误了吗?如果是这样,您应该将其包含在您的问题中。作为单元测试设置的一部分,您可能能够摆脱初始化 SessionAdapter 静态类的属性。但实际上,这听起来像是一个外部依赖项,不应该是静态的——相反,您应该将其设为非静态类并将其作为依赖项注入,就像您对用户和配置文件服务所做的那样。这将允许您模拟它以进行单元测试。编写可测试的代码很重要,静态类不适合可测试的代码。
-
顺便说一句,您的
expected字符串不是有效的 JSON,假设它运行完成,您的操作方法几乎肯定会生成有效的 JSON。您需要解决此问题才能使测试成功。
标签: c# .net asp.net-mvc unit-testing moq