本文介绍了如何成功构建IdentityResult == true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注入了Microsoft.AspNet.Identity.UserManager的类,我希望userManager.CreateAsync(user,password)方法返回一个Task,其中IdentityResult.Succeeded = true.但是,IdentityResult唯一可用的构造函数是会导致Succeeded属性为false的失败构造函数.

I have a class with Microsoft.AspNet.Identity.UserManager injected, and I want to expect the userManager.CreateAsync(user, password) method to return a Task where the IdentityResult.Succeeded = true. However, the only available constructors for IdentityResult are failure constructors that will cause Succeeded property to be false.

如何创建成功== true的IdentityResult? IdentityResult没有实现接口,并且Succeeded不是虚拟的,因此我看不到任何通过Rhino Mocks(我将其用作模拟框架)创建模拟对象的明显方法.

How does one create an IdentityResult that has Succeeded == true? IdentityResult doesn't implement an interface and Succeeded isn't virtual so I don't see any obvious ways of creating a mock object through Rhino Mocks (which i'm using as my mocking framework).

我的方法执行以下操作.提供此示例以说明为什么我可能要对此进行模拟.

My method does something like the below. Providing this example to show why I might want to mock this.

  public async Task<IdentityResult> RegisterUser(NewUser newUser)
    {
        ApplicationUser newApplicationUser = new ApplicationUser()
        {
            UserName = newUser.UserName,
            Email = newUser.Email
        };

        IdentityResult identityResult = await applicationUserManager.CreateAsync(newApplicationUser, newUser.Password);

        if(identityResult.Succeeded)
        {
            someOtherDependency.DoSomethingAmazing();
        }

        return identityResult;
    }

我正在尝试编写一个单元测试,以确保如果identityResult.Succeeded为true,则调用someOtherDependency.DoSomethingAmazing().感谢您的帮助!

I'm trying to write a unit test that ensures that someOtherDependency.DoSomethingAmazing() is called if identityResult.Succeeded is true. Thanks for any help!

推荐答案

静态IdentityResult.Success属性是否可以正常工作? http://msdn.microsoft.com/zh-CN/library/microsoft.aspnet.identity.identityresult.success(v=vs.108).aspx

Would the static IdentityResult.Success property work? http://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.identityresult.success(v=vs.108).aspx

修改:要添加更多细节,似乎您想要做的是让模拟的CreateAsync返回其中Suceeded为true的IdentityResult.为此,我只是从您的模拟返回IdentityResult.Success.不需要模拟IdentityResult本身.

Edit:To add some more detail, it seems what you want to do is get your mocked CreateAsync to return an IdentityResult where Suceeded is true. For that I would just return IdentityResult.Success from your mock. There's shouldn't be a need to mock the IdentityResult itself.

示例:如何设置返回成功身份结果的服务.

Example: How to setup a service that returns Successful identity result.

    applicationUserManagerMock.Setup(s => 
        s.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>())
    ).ReturnsAsync(IdentityResult.Success);

这篇关于如何成功构建IdentityResult == true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:43