如何在执行模拟时解决虚拟成员错误的无效设置

时间:2017-01-29 09:20:58

标签: c# .net asp.net-mvc unit-testing moq

我正在使用moqs进行单元测试,我收到错误,如虚拟成员错误的无效设置。我使用服务来注入依赖项,我的sessioninfo是一个类

控制器方法是:

public PartialViewResult _AddOrgStructure()
{
    int organizationID = _sessionAdapterService.Instance.Organization.id; 
    OrganizationViewModels _Organization = getOrgData(organizationID);
    return PartialView("_AddOrgStructure", _Organization);
}

SessionAdapterClass.cs

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 void Organization_AddOrgStructure_Test()
{
    //Arrange
    controller = new OrganizationController(organizationServiceMock.Object, memberInfoServiceMock.Object, userServiceMock.Object, sessionadapterserviceMock.Object);

    var mockControllerContext = new Mock<ControllerContext>();
    var mockSession = new Mock<HttpSessionStateBase>();
            mockSession.SetupSet(s => s["sessionInfoKey"] = "11").Verifiable(); //somevalue
    mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object);
    mockControllerContext.SetupGet(p => p.HttpContext.Session["sessionInfoKey"]).Returns("Hello World");
    controller.ControllerContext = mockControllerContext.Object;

    List<Organization> _Organization = new List<Organization>();

    organizationServiceMock.Setup(x => x.GetOrganization(1)).Returns(_Organization);

    sessionadapterserviceMock.SetupGet(m => m.Instance.Id).Returns("1");

    //Act
    var result = controller._AddOrgStructure();
    //var viewResult = result as PartialViewResult;

    //Assert
    //Assert.AreEqual("_AddOrgStructure", viewResult.ViewName);
}

我的sessionInfo类是

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

0 个答案:

没有答案