如何编写单元测试用例来访问存储库数据以在c#中测试

时间:2018-04-12 02:51:07

标签: c# unit-testing repository

这是我写给test.In的测试用例在用户存储库中获取null值,如何将值从测试用例传递给特定的构造函数来赋值。

[的TestFixture]     公共类UserPageTest

{

    UserManagement addUser;
    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;


    IEntityBaseRepository<User> userRepository;
    IEntityBaseRepository<Role> roleRepository;
    IEntityBaseRepository<UserStatus> userStatusRepository;
    IUnitOfWork unitOfWork;
    IEncryptionService encryptionService;
    IEntityBaseRepository<ForgotPassword> forgotPassword;

    public UserPageTest(IEntityBaseRepository<User> userRepository,
        IEntityBaseRepository<Role> roleRepository,
        IEntityBaseRepository<UserStatus> userStatusRepository,
        IUnitOfWork unitOfWork,
        IEncryptionService encryptionService,
        IEntityBaseRepository<ForgotPassword> forgotPassword)
    {
        addUser = new UserManagement(userRepository, roleRepository,userStatusRepository,
                                    unitOfWork, encryptionService, forgotPassword);
    }



    [Test]

    public void AddUser()
    {



        User userDetail = new User();
        userDetail.FirstName = "bnb";
        userDetail.LastName = "hjj";
        userDetail.Username = "sff";
        userDetail.Email = "grs@gmail.com";
        userDetail.RoleId =1 ;
        addUser = new UserManagement(userRepository, roleRepository, userStatusRepository,
                                  unitOfWork, encryptionService, forgotPassword);

        addUser = new UserManagement();

        Assert.AreEqual(true, addUser.AddUser(userDetail, "admin").IsSuccess);

    }
}

这是我需要访问和测试的类。

构造

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;

    }

方法:

在此用户存储库中获取null value.used控制器。

public ResponseDTO AddUser(User user, string 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();
                    }

0 个答案:

没有答案