Asp.net Identity在没有HttpContext的情况下创建User

时间:2014-06-26 15:31:56

标签: 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...
    }
}

我该怎么称呼:

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

启动Startup以便我可以创建此用户吗?

或者我完全走错了路?

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

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

1 个答案:

答案 0 :(得分:11)

想出了一种用我的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;
        }
    }