我嘲笑抽象類來測試一個類的具體方法如下。我如何模擬UserManager和RoleManager進行單元測試
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("[email protected]", true);
問題: 我的抽象類使用Entity Framework 6 and Asp.Net Identity UserManager and RoleManager
如下
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;
}
}
如何我可以在上面的代碼嘲弄嘲弄TestUserManager and TestRoleManager
?
我嘗試了以下方法,但無法獲得與controllerContext掛鉤的方式。
var userStore = new Mock<IUserStore<TestUser>>();
var userManager = new TestUserManager(userStore.Object);
答覆問題問之下由阿列克謝L.
這是怎麼TestUserManager
派生UserManaer
和實施。
public class TestUserManager : UserManager<TestUser>
{
public TestUserManager(IUserStore<TestUser> store)
: base(store)
{
}
public static TestUserManager Create(IdentityFactoryOptions<TestUserManager> options, IOwinContext context)
{
TestUserManager manager = new TestUserManager(new UserStore<TestUser>(context.Get<AuthContext>()));
setValidationRules(manager);
IDataProtectionProvider dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider == null)
dataProtectionProvider = new DpapiDataProtectionProvider();
manager.UserTokenProvider = new DataProtectorTokenProvider<TestUser>(dataProtectionProvider.Create("ASP.NET Identity")) { TokenLifespan = TimeSpan.FromDays(expiryTime) };
return manager;
}
public static TestUserManager CreateLocal(AuthContext context)
{
TestUserManager manager = new TestUserManager(new UserStore<TestUser>(context));
setValidationRules(manager);
IDataProtectionProvider dataProtectionProvider = new DpapiDataProtectionProvider();
manager.UserTokenProvider = new DataProtectorTokenProvider<TestUser>(dataProtectionProvider.Create("ASP.NET Identity"));
return manager;
}
private static void setValidationRules(ApplicationUserManager manager)
{
manager.UserValidator = new UserValidator<TestUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new ApplicationPasswordValidator
{
RequiredLength = 30,
MaximumCharacters = 30,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false
};
}
}
控制器代碼
public async Task<IHttpActionResult> ActivateDeactive(string studentId, bool active)
{
IdentityResult result;
_accountService = new AccountMgtService(UserService, RoleService);
result = await _accountService.ActiveDeactiveUser(userId, active);
}
顯示控制器如何使用'UserManager' –
請檢查我的更新問題 - 最後的答案您題。 – immirza
我的意思是控制器的代碼。調用'UserManager'的代碼 –