mercredi 27 juillet 2016

How do i mock UserManager and RoleManager for unit test

I have mocked abstract class to test concrete method of a class as following.

var mock = new Mock<BaseTestController>();
mock.CallBase = true;
var ta = mock.Object;

ta.ControllerContext = new HttpControllerContext { Request = new HttpRequestMessage() };          
var owinMock = new Mock<IOwinContext>();
owinMock.Setup(o => o.Authentication.User).Returns(new ClaimsPrincipal());
owinMock.Setup(o => o.Request).Returns(new Mock<OwinRequest>().Object);
owinMock.Setup(o => o.Response).Returns(new Mock<OwinResponse>().Object);
owinMock.Setup(o => o.Environment).Returns(new Dictionary<string, object> { { "key1", 123 } });
var traceMock = new Mock<TextWriter>();
owinMock.Setup(o => o.TraceOutput).Returns(traceMock.Object);

ta.Request.SetOwinContext(owinMock.Object);
var result = await ta.ActivateDeactive("test@user.com", true);

Problem: My abstract class use Entity Framework 6 and Asp.Net Identity UserManager and RoleManager as following

public TestUserManager UserService
{
    get
    {
        return _userService ?? Request.GetOwinContext().GetUserManager<TestUserManager>();
    }

    private set
    {
        _userService = value;
    }
}

public TestRoleManager RoleService
{
    get
    {
        return _roleService ?? Request.GetOwinContext().Get<TestRoleManager>();
    }

    private set
    {
        _roleService = value;
    }
}

How i can mock TestUserManager and TestRoleManager in my above mocking code?

I tried the following way but couldn't get the way to hook it up with controllerContext.

var userStore = new Mock<IUserStore<TestUser>>();
var userManager = new TestUserManager(userStore.Object);

Aucun commentaire:

Enregistrer un commentaire