我试图对与this.HttpContext.Authentication
直接交互的MVC 6控制器进行单元测试以进行身份验证。我没有使用Identity Framework,而是直接与Cookie身份验证中间件进行交互。
我已经看过MS的一些例子,我一直在使用DefaultHttpContext进行测试。问题是DefaultHttpContext上的AuthenticationManager属性是只读的,因此我不确定如何模拟它。
如果我不嘲笑它,我会收到错误声明"没有配置身份验证处理程序来处理该方案:Cookies"在我的测试中呼叫:
HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, authProperties);
答案 0 :(得分:7)
我遇到了同样的问题,我终于做了一些嘲弄 我用Moq
var claimPrincipal = new ClaimsPrincipal();
var mockAuth = new Mock<AuthenticationManager>();
mockAuth.Setup(c => c.SignInAsync("YourScheme", claimPrincipal)).Returns(Task.FromResult("done"));
var mockContext = new Mock<HttpContext>();
mockContext.Setup(c => c.Authentication).Returns(mockAuth.Object);
var fakeActionContext = new ActionContext(mockContext.Object, new RouteData(), new ControllerActionDescriptor());
var contContext = new ControllerContext(fakeActionContext);
然后将该控制器上下文传递给您要测试的控制器
var controller = new TestedController(Your attribute)
{
ControllerContext = contContext
};
答案 1 :(得分:3)
MS建议不要使用AuthenticationManager而是使用扩展方法,不推荐接受答案。另一种选择如下。请注意,添加的其他两个服务是因为设置RequestServices会删除一些默认值,否则这些默认值将由DefaultHttpContext创建。
var context = new ControllerContext();
context.HttpContext = new DefaultHttpContext();
context.HttpContext.User = userMock.Object; // If needed
context.HttpContext.Session = sessionMock.Object; // If needed
var authManager = new Mock<IAuthenticationService>();
authManager.Setup(s => s.SignOutAsync(It.IsAny<HttpContext>(),
CookieAuthenticationDefaults.AuthenticationScheme,
It.IsAny<Microsoft.AspNetCore.Authentication.AuthenticationProperties>())).
Returns(Task.FromResult(true));
var servicesMock = new Mock<IServiceProvider>();
servicesMock.Setup(sp => sp.GetService(typeof(IAuthenticationService))).Returns(authManager.Object);
servicesMock.Setup(sp => sp.GetService(typeof(IUrlHelperFactory))).Returns(new UrlHelperFactory());
servicesMock.Setup(sp => sp.GetService(typeof(ITempDataDictionaryFactory))).Returns(new TempDataDictionaryFactory(new SessionStateTempDataProvider()));
context.HttpContext.RequestServices = servicesMock.Object;
// Use context to pass to your controller
答案 2 :(得分:0)
这是使用FakeItEasy的方式:
var actionContext = new ActionContext
{
HttpContext = new DefaultHttpContext(),
RouteData = new RouteData(),
ActionDescriptor = new ControllerActionDescriptor()
};
// Creating AuthenticationService and Return calls for the Authentication factory
var authManager = A.Fake<IAuthenticationService>();
var authProperties = new Microsoft.AspNetCore.Authentication.AuthenticationProperties();
var servicesMock = A.Fake<IServiceProvider>();
A.CallTo(() => authManager.SignOutAsync(actionContext.HttpContext, CookieAuthenticationDefaults.AuthenticationScheme, authProperties)).Returns(Task.FromResult(true));
A.CallTo(() => servicesMock.GetService(typeof(IAuthenticationService))).Returns(authManager);
A.CallTo(() => servicesMock.GetService(typeof(IUrlHelperFactory))).Returns(new UrlHelperFactory());
A.CallTo(() => servicesMock.GetService(typeof(ITempDataDictionaryFactory))).Returns(new TempDataDictionaryFactory(new SessionStateTempDataProvider()));
var concon = new ControllerContext(actionContext);
concon.HttpContext.RequestServices = servicesMock;