How to Construct IdentityResult With Success == true

一笑奈何 提交于 2019-11-30 16:57:55

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

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.

To make the Succeeded property equal to True use either of these examples:

return IdentityResult.Success;
IdentityResult result = IdentityResult.Success;

Further more, to make the Success property returns true

return Identity.Success;

Then in your implementing code, call the implementing method like this

var result = await RegisterUser(newUser).Result
if(result.Succeeded)
{
  //do something
}

Approach is different for Microsoft.AspNet.Identity and Microsoft.AspNetCore.Identity. Bellow I provided solutions for both:

  1. If you are using Microsoft.AspNet.Identity namespace please check following approach:

    In my example I mocked IdentityResult.Success in following way:

    First, IdentityResult has protected constructor:

    protected IdentityResult(bool success);
    

    Protected constructor can be accessed from inherited class if you implement like this:

        public class IdentityResultMock : IdentityResult
        {
            public IdentityResultMock(bool succeeded) : base(succeeded) { }
        }
    

    Second, in my unit test I configured ResetPasswordAsync() to return identityResult like in below:

    var identityResult = new IdentityResultMock(true);
    _mockUserManager.Setup(e => e.ResetPasswordAsync(user.Id, model.Code, model.Password)).ReturnsAsync(identityResult);
    

    Third, in my controller's action ResetPasswordAsync() will returns result with Successed == true:

    var result = await userManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
    

    Basically I implemented new class which inherited IdentityResult and used that in my test cases.

  2. If you are using using Microsoft.AspNetCore.Identity namespace then try following approach:

    IdentityResult in this case does't have protected constructor but it has protected property Succeeded which can be accessed within class who inherited IdentityResult.

    We can achieve like in following example:

    public class IdentityResultMock : IdentityResult
    {
        public IdentityResultMock(bool succeeded = false)
        {
            this.Succeeded = succeeded;
        }
    }
    
    var result = new IdentityResultMock(true);
    Console.WriteLine(result.Succeeded);
    
    result.Succeeded = true; // This is not allowed! Please use constructor.
    
var responseTask = System.Threading.Tasks.Task.FromResult(IdentityResult.Success);

_mocAuthService.Stub(s => s.RegisterUser(null)).IgnoreArguments().Return(responseTask);

If you need a successful IdentityResult then all you need to do is call the Success property. This will give you a successful IdentityResult.

https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.identityresult(v=vs.108).aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!