I have the problem that i want to write a unit test of a custom function in my user service. But this user service derives from asp.net core identity user manager. I want to extend the functionality of the user manager therefore i derived from it. So I want to test the method CreateCRohMUserAsync
public class UserService : UserManager<User>, IUserService
{
public UserService(IUserStore<User> store,
IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<User> passwordHasher,
IEnumerable<IUserValidator<User>> userValidators,
IEnumerable<IPasswordValidator<User>> passwordValidators,
ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<User>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public async Task<IdentityResult> CreateCRohMUserAsync(User user)
{
PasswordGenerator pwGenerator = new PasswordGenerator();
var password = pwGenerator.Generate();
user.UserName = GetUniqueUserName(user.FirstName, user.LastName);
var result = await base.CreateAsync(user, password);
if (result.Succeeded)
{
//TODO: send mail to created user
}
return result;
}
public string GetUniqueUserName(string firstName, string lastName)
{
if (string.IsNullOrEmpty(firstName))
{
throw new ArgumentNullException(nameof(firstName), "firstName can not be null");
}
if (string.IsNullOrEmpty(lastName))
{
throw new ArgumentNullException(nameof(lastName), "lastName can not be null");
}
return lastName + firstName.Substring(0, 2) + Users.Count() + 1;
}
}
}
In my unit test i want to mock the base.CreateAsync(user,password) and the Users.Count() functionality of my base class. But I don't know how to do this. Or is my implementation wrong? How can I unit test this mehtod?