我编写了单元测试用例来测试添加用户方法。当我模拟unitofwork并通过构造函数传递它。它显示错误,例如无法实例化class的代理。找不到无参数构造函数。
我需要测试的代码构造函数:
public class UserManagement : IUserManagement
{
#region Property declarations
private readonly IEntityBaseRepository<User> _userRepository;
private readonly IEntityBaseRepository<Role> _roleRepository;
private readonly IEntityBaseRepository<UserStatus> _userStatusRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly IEncryptionService _encryptionService;
private readonly IEntityBaseRepository<ForgotPassword> _forgotPassword;
#endregion
public UserManagement(
IEntityBaseRepository<User> userRepository,
IEntityBaseRepository<Role> roleRepository,
IEntityBaseRepository<UserStatus> userStatusRepository,
IUnitOfWork unitOfWork,
IEncryptionService encryptionService,
IEntityBaseRepository<ForgotPassword> forgotPassword)
{
this._userRepository = userRepository;
this._roleRepository = roleRepository;
this._userStatusRepository = userStatusRepository;
this._unitOfWork = unitOfWork;
this._encryptionService = encryptionService;
this._forgotPassword = forgotPassword;
}
方法: 我已经通过构造函数传递了工作单元,但它仍然在_unitOfwork.commit()
中断开public ResponseDTO AddUser(用户用户,字符串currentUserName) { var response = new ResponseDTO();
try
{
User currentUser = this._userRepository.GetUserByUserName(currentUserName.Trim().ToLower());
if (user != null)
{
if (!this._userRepository
.FindBy(s => s.Email.Trim().ToLower() == user.Email.ToLower().Trim() && s.IsDeleted == false)
.Any())
{
if (!this._userRepository.FindBy(
s => !string.IsNullOrEmpty(user.MobileNumber)
&& s.MobileNumber == user.MobileNumber && s.IsDeleted == false).Any())
{
var newUser = new User()
{
LastName = user.LastName,
FirstName = user.FirstName,
Username = user.Username,
Email = user.Email,
HashedPassword = user.HashedPassword,
Salt = user.Salt,
AreaCode = user.AreaCode,
MobileNumber = user.MobileNumber,
RoleId = user.RoleId,
Address = user.Address,
CreatedByUserId = currentUser.Id,
UserStatusId = (int)Enumerations.UserStatus.Active
};
this._userRepository.Add(newUser);
this._unitOfWork.Commit();
}
else
{
response.IsSuccess = false;
response.UserMessage = "Contact number is already in use.";
}
}
else
{
response.IsSuccess = false;
response.UserMessage = "The email is already in use.";
}
}
response.IsSuccess = true;
response.UserMessage = user.Username + " user created successfully";
}
catch (Exception ex)
{
response.IsSuccess = false;
response.TechMessage = ex.Message;
}
return response;
}
Test case :
public void AddUser()
{
var userRep = new Mock<IEntityBaseRepository<User>>();
var roleRep = new Mock<IEntityBaseRepository<Role>>();
var userStatusRep = new Mock<IEntityBaseRepository<UserStatus>>();
var unitOfWork = new Mock<UnitOfWork>();
var encryptRep = new Mock<IEncryptionService>();
var forgetPasswordRep = new Mock<IEntityBaseRepository<ForgotPassword>>();
var sut = new UserManagement(userRep.Object, roleRep.Object, userStatusRep.Object, unitOfWork.Object, encryptRep.Object, forgetPasswordRep.Object);
User userDetail = new User();
userDetail.FirstName = "jkkj";
userDetail.LastName = "jjk";
userDetail.Username = "sff";
userDetail.Email = "grs@gmail.com";
userDetail.RoleId = 1;
Assert.AreEqual(true, sut.AddUser(userDetail, "superadmin").IsSuccess);
}
答案 0 :(得分:-1)
为什么在以下行中传递null:
var unitOfWork = new Mock(null);
尽量不要在这里传递null,或者你可以在UnitOfWork类中定义一个参数less constructor。