【问题标题】:Microsoft Fakes HttpContext.User.Identity [duplicate]Microsoft 伪造 HttpContext.User.Identity [重复]
【发布时间】:2014-08-30 12:07:05
【问题描述】:

我和这篇文章有类似的问题...

How can use Microsoft Fakes to mock User.Identity.Name

在尝试遵循给出的答案之一时,我在创建其中一个对象 StubIPrincipal 时遇到了问题。

我已经为 System.Web 添加了 Fakes。在帖子之后,我还添加了 Fakes for System。当我仍然无法创建存根时,我还为 IPrincipal 所在的 System.Security 添加了 Fakes。创建用户仍然没有运气。

这是我用来尝试存根 HttpContext 的方法,该方法有效。我还需要能够访问 HttpContext.User.Identity。

    public static HttpContextBase FakeHttpContext(string url = "")
    {
        var cookies = new HttpCookieCollection();
        var session = new StubHttpSessionStateBase();
        var server = new StubHttpServerUtilityBase();
        var items = new ListDictionary();
        var user = new StubIPrincipal // Cannot resolve symbol 'StubIPrincipal'
            {

            };
        var browser = new StubHttpBrowserCapabilitiesBase
            {
                IsMobileDeviceGet = () => false
            };
        var request = new StubHttpRequestBase
            {
                AppRelativeCurrentExecutionFilePathGet = () => url,
                ApplicationPathGet = () => url,
                CookiesGet = () => cookies,
                UserAgentGet = () => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
                BrowserGet = () => browser,
                UrlGet = () => !string.IsNullOrEmpty(url) ? new Uri(url) : null
            };
        var response = new StubHttpResponseBase
            {
                CookiesGet = () => cookies,
                ApplyAppPathModifierString = s => s,
                StatusCodeGet = () => (int)HttpStatusCode.OK
            };
        var context = new StubHttpContextBase
            {
                RequestGet = () => request,
                ResponseGet = () => response,
                SessionGet = () => session,
                ServerGet = () => server,
                ItemsGet = () => items
            };

        return context;
    }

我尝试的另一件事是手动创建用户并将其添加到上下文中。

System.Security.Principal.IPrincipal user;
var winId = new WindowsIdentity("u1332304");
user = new WindowsPrincipal(winId);
context.User = user;

当我这样做时,我在 context.User 对象上收到一个 Not Implemented 错误。

【问题讨论】:

    标签: c# unit-testing .net-4.0 httpcontext microsoft-fakes


    【解决方案1】:

    为了让我存根IPrincipal,我不得不伪造System,这反过来又添加了mscorlib.fakes。这使我能够在我的代码中使用System.Security.Principal.Fakes.StubIPrincipal

    然后我通过执行以下操作对User.Identity.Name 进行了存根:

    using (AccountController controller = new AccountController())
    {
        StubHttpContextBase stubHttpContext = new StubHttpContextBase();
    
        controller.ControllerContext = new ControllerContext(stubHttpContext, new RouteData(), controller);
    
        StubIPrincipal principal = new StubIPrincipal();
        principal.IdentityGet = () =>
        {
            return new StubIIdentity 
            { 
                NameGet = () => "bob" 
            };
        };
        stubHttpContext.UserGet = () => principal;
    }
    

    【讨论】:

      【解决方案2】:

      您可以在不使用 Microsoft Fakes(或任何模拟库,就此而言)的情况下模拟 HttpContext.Current.User。请参阅上一个问题(免责声明:由我回答):Mock HttpContext.Current in Test Init Method

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-18
        • 1970-01-01
        • 2022-11-11
        • 2012-07-08
        • 2020-09-09
        • 2013-05-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多