0

This is the test case that I wrote to test.In gets null value in user repository,how to pass the value from test case to particular constructor to assign value.

[TestFixture] public class 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);

    }
}

This is the class which I need to access and test.

constructor:

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;

    }

Method:

In this user repository gets null value.used controller also.

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();
                    }
DotNetUser
  • 415
  • 3
  • 18
  • Where are you initialising userRepository in your test class? – Tej Apr 12 '18 at 03:03
  • In test class,I have declared and assigned value in constructor to pass it.I don't know how to initialize as the values are getting from controller. – DotNetUser Apr 12 '18 at 03:49
  • You need to assign values to these using Mocks that will simulate behaviour and then pass the data to constructor. If you are not assigning any value then it is bound to throw null pointer exception – Tej Apr 12 '18 at 03:57
  • can you suggest me how to mock user repository.I don't have any idea.I am new to testing – DotNetUser Apr 12 '18 at 04:04
  • Please go ahead and check this question and read relevant material https://stackoverflow.com/questions/2098937/proper-way-to-mock-repository-objects-for-unit-tests-using-moq-and-unity – Tej Apr 12 '18 at 04:34
  • This link helped me a lot.Now,I get the value in user repository. But Now I can't access to unitofwork and encryption service.I tried to mock and pass through constructor.It shows can not instantiate proxy of class. – DotNetUser Apr 12 '18 at 06:35

0 Answers0