有什么方法可以模拟ASP.NET MVC Web应用程序中的Claims Principal? [英] Is there any way I can mock a Claims Principal in my ASP.NET MVC web application?
问题描述
我有一些ASP.NET MVC
控制器代码,用于检查用户是否已通过身份验证,如果通过了验证,它将检查是否具有特定的声明.效果很好.
I've got some ASP.NET MVC
controller code that checks if a user is authenticated and if so, it checks to see if it has a specific claim. Works fine.
我有一些单元测试,我需要模拟一个IPrincipal
(这很容易做到)...但是我不确定如何检查声明!我通常会做类似的事情
I've got some unit tests and I need to mock out an IPrincipal
(which is easy to do) ... but I'm not sure how to check for the claims! I usually do something like
public static ClaimsPrincipal ClaimsPrincipal(this Controller controller)
{
return controller.User as ClaimsPrincipal;
}
和一些控制器代码...
and some controller code ...
this.ClaimsPrincipal().HasClaim(x => x.......);
但是当我在单元测试中对它进行测试时,这一切都失败了,因为我不确定如何mock
ClaimsPrincipal
but this all fails when I test this in my Unit Test .. because I'm not sure how I can mock
the ClaimsPrincipal
有什么想法吗?
推荐答案
大多数方法都是虚拟的,因此它们是可模拟的.
Also most of the methods are virtual so those are mock-able.
这篇关于有什么方法可以模拟ASP.NET MVC Web应用程序中的Claims Principal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!