Home > Blockchain >  Is there a better way to test this ASP.NET MVC application?
Is there a better way to test this ASP.NET MVC application?

Time:12-28

I have created mocks for both IUserService and IDataResult. Test works fine but instead of pulling from database, I created user object to test this getcustomerlogin method. Is there a better way to test this case? Can we test this method with actual data from database?

This is the testing code:

namespace UnitTesting
{
    public class Tests
    {
        [Test]
        public void login_unit_test()
        {
            // arrange
            var userinput = new UserForLogin()
            {
                email = "[email protected]",
                password = "123456"
            };

            var userobject = new User()
            {
                Email= "[email protected]",
                Password = "123456"
            };

            var mockIdataResult = new Mock<IDataResult<User>>();
            mockIdataResult.Setup(i => i.Success).Returns(true);
            mockIdataResult.Setup(i => i.Data).Returns(userobject);

            var mockIUserService = new Mock<IUserService>();
            mockIUserService.Setup(i => i.getByEmail(userinput)).Returns(mockIdataResult.Object);
            var authscontroller = new AuthsController(mockIUserService.Object);

            // action
            IActionResult result = authscontroller.getcustomerlogin(userinput);
            var okResult = result as OkObjectResult;

            // assert
            Assert.AreEqual(200, okResult.StatusCode);
        }
    }
}

This is the login function we are trying to test.

namespace WEBAPII.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthsController : ControllerBase
    {
        IUserService _userService;

        public AuthsController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost("login")]
        public IActionResult getcustomerlogin(UserForLogin userForLogin)
        {
            var user = _userService.getByEmail(userForLogin);

            if (user.Success)
            {
                if (!(user.Data.Email == userForLogin.email && 
                      user.Data.Password == userForLogin.password))
                {
                    return BadRequest(user);
                }

                return Ok(user);
            }

            return BadRequest(user);
        }

        [HttpPost("logindadmin")]
        public IActionResult adminlogin(UserForLogin userForLogin)
        {
            var admin = _userService.getAdmin(userForLogin);

            if (admin.Success)
            {
                return Ok(admin);
            }

            return BadRequest(admin);
        }
    }
}

This is the IUserService interface that is set inside Authscontroller

namespace Business.Abstract
{
    public interface IUserService
    {
        List<User> GetAll();
        User GetById(int userId);
        void Add(User user);

        IDataResult<User> getByEmail(UserForLogin userForLogin);
        IDataResult<User> getAdmin(UserForLogin userForLogin);
    }
}

This is UserForLogin class that takes user information parameters.

public class UserForLogin
{
    public string email { get; set; }
    public string password { get; set; }
}

This is the User class that we store our information:

namespace Entities.Concrete
{
    public class User : IEntity
    {
        public int id { get; set; }
        public string Email { get; set; }

        public string Password { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public DateTime CreatedAt { get; set; }

        public string roles { get; set; }
    }
}

CodePudding user response:

To test your functionality with actual data from the database, you need to implement an integration test instead of a unit test.

You can create a new test database or a replica of the existing production server and populate some test data into it.

Use EF or ADO.Net for DB operations in place of macking.

  • Related