c# - Asp.net Identity 在没有 HttpContext 的情况下创建用户

标签 c# asp.net-mvc visual-studio nunit asp.net-identity

我正在尝试运行一些单元测试,我希望能够使用 Asp.Net Identity 框架创建一个用户,但它需要一个 HttpContextBase。所以,我决定使用另一个 Stack Overflow 线程建议并模拟一个。它看起来像这样:

    public HttpContext FakeHttpContext
    {
        get
        {
            var httpRequest = new HttpRequest("", "http://stackoverflow/", "");
            var stringWriter = new StringWriter();
            var httpResponce = new HttpResponse(stringWriter);
            var httpContext = new HttpContext(httpRequest, httpResponce);

            var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
                                                    new HttpStaticObjectsCollection(), 10, true,
                                                    HttpCookieMode.AutoDetect,
                                                    SessionStateMode.InProc, false);

            httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
                                        BindingFlags.NonPublic | BindingFlags.Instance,
                                        null, CallingConventions.Standard,
                                        new[] { typeof(HttpSessionStateContainer) },
                                        null)
                                .Invoke(new object[] { sessionContainer });

            return httpContext;
        }
    }

    public HttpContextBase FakeHttpContextBase
    {
        get
        {
            return (new HttpContextWrapper(this.FakeHttpContext));
        }
    }

这工作正常,直到它到达 Owin 的东西,此时它失败了。

Startup 不运行单元测试。这是我的创业公司:

    [assembly: OwinStartupAttribute(typeof(MyProject.Startup))]
    namespace MyProject
    {
        public partial class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                ConfigureAuth(app);
            }
        }

        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context and user manager to use a single instance per request
            app.CreatePerOwinContext(DataContext.Create);
            app.CreatePerOwinContext<IdentityManager>(IdentityManager.Create);

            // More Identity Stuff...
        }
    }

How can I call:

    var result = await IdentityManager.Instance(this.FakeHttpContextBase).CreateAsync(user, password);

然后让 Startup 运行以便我可以创建这个用户?

还是我在这方面完全走错了路?

我正在尝试在 Visual Studio 中使用 NUnit 运行单元测试。

注意:请不要告诉我应该使用模拟库。 Linq to Object 与 Linq to Entity 的工作方式不同,我正在尝试测试我将在我的应用程序中使用的实际代码,这意味着针对实际数据库对其进行测试。这一切都很好。这只是关于如何创建用户。

最佳答案

想出一种不同的方法来仅使用我的 DataContext 来实例化用户管理器。

这是最终的测试结果:

    private async void SeedUser()
    {
        using (var context = new DataContext()) {
            var newUser = new User() { UserName = "buzzlightyear@pixar.com", Email = "buzzlightyear@pixar.com", Name = "Buzz Lightyear" };

            var userManager = new IdentityManager(new UserStore<User>(context));

            var result = await userManager.CreateAsync(newUser, "infinityandbeyond");

            if (!result.Succeeded) {
                Assert.Fail("Failed to set up User for TestBase.");
            }

            var user = context.Users.FirstOrDefault();

            if (user == null) {
                Assert.Fail("The User was not found in the database.");
            }

            this.UserId = user.Id;
        }
    }

关于c# - Asp.net Identity 在没有 HttpContext 的情况下创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24434611/

相关文章:

c# - Azure Blob 存储 blob 到索引

c# - MVC3 和 URL(使用区域,构建 <a> 标签有问题)

c# - 从 WebApi 返回图像

c# - 如何在没有打开/保存对话框的情况下播放 Telerik RadCaptcha .wav 文件

javascript - 我需要忽略 GIT 中的 .cache 文件夹吗

c# - WPF - 创建带有图标的 ListView

ASP.NET Bundles 如何禁用缩小

c++ - 在 OpenCV 中将彩色图像转换为灰度问题

c# - 如何启用Visual Studio(windows)和Docker(linux-image)在同一文件夹中同时运行?

javascript - jquery.click 之后更新 HTML 页面