问题
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.
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;
}
I'm trying to write a unit test that ensures that someOtherDependency.DoSomethingAmazing() is called if identityResult.Succeeded is true. Thanks for any help!
回答1:
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.
回答2:
To make the Succeeded property equal to True use either of these examples:
return IdentityResult.Success;
IdentityResult result = IdentityResult.Success;
回答3:
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
}
回答4:
Approach is different for Microsoft.AspNet.Identity and Microsoft.AspNetCore.Identity. Bellow I provided solutions for both:
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 withSuccessed == 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.
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 propertySucceeded
which can be accessed within class who inheritedIdentityResult
.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.
回答5:
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
来源:https://stackoverflow.com/questions/26269104/how-to-construct-identityresult-with-success-true