How to write unit testing in the console application in C#?

We need a major .net library to in our .net console project for unit testing any framework.

Git Source: https://github.com/jayamoorthi/UnitTestInConsole

.NET Packages

Microsoft.NET.Test.Sdk

Xunit

xunit.runner.visualstudio        

additionally, you have to include specific testing framework library to use like FakeItEasy, Moq, NSubstitude and JustMock to writing test case ?using Xunit/ Nunit/ MSTest.

Implementation using Console project

Create a new console project with Name “UnitTestInConsole”

In my project I am going to use FakeItEasy ?


Create a user model class

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string MailId { get; set; }
}        

Create a UserRepository Interface

public interface IUserRepository
{
    Task<List<User>> GetAllAsync();
}        

UserRepository implementation with static data for this GetAllAsync()

public class UserRepository : IUserRepository
{
    public async Task<List<User>> GetAllAsync()
    {
        return await Task.FromResult(new List<User>
        {
            new User(){ Id = Guid.NewGuid(), MailId = "[email protected]", Name = " Moorthi"},
            new User(){ Id = Guid.NewGuid(), MailId = "[email protected]", Name = " Moorthi1"}
        });
    }
}        

Create a IUserService interface for concreate UserService implementation class

public interface IUserService
{
    Task<List<User>> GetAllAsync();
}        

public class UserService : IUserService
{
    private readonly IUserRepository _userRepository;
    public UserService(IUserRepository userRepository) 
    {
        _userRepository = userRepository;
    }
    public async Task<List<User>> GetAllAsync()
    {
       return await _userRepository.GetAllAsync();
    }
}        

Create a UserServiceTest class for writting the unit test case.

  public class UserServiceTest
    {
        private readonly IUserRepository userRepositoryFake; 
        private readonly UserService _userService;
        public UserServiceTest()
        {
            userRepositoryFake = A.Fake<IUserRepository>();
            _userService = new UserService(userRepositoryFake);

        }


        [Fact]
        public async Task GetAllAsync_Positive_UserRecordHasExists_ShouldReturnAllUsers()
        {
            //Arrange

            var users =  new List<User>
            {
                new User(){ Id = Guid.NewGuid(), MailId = "[email protected]", Name = " jaya"},
                new User(){ Id = Guid.NewGuid(), MailId = "[email protected]", Name = " Moorthi1"}
            };

            A.CallTo(() => userRepositoryFake.GetAllAsync()).Returns(users);

            //Act
            var res = await _userService.GetAllAsync();


            //Assert
            Assert.IsType<List<User>>(res);
            Assert.Equal(2, res.Count());
          
        }
    }
}
        

creating mock IUserRepository for using FakeItEasy

userRepositoryFake = A.Fake<IUserRepository>();        

CallTo method to set mock values to specific method using CallTo and Returns ().

A.CallTo(() => userRepositoryFake.GetAllAsync()).Returns(users);        

call the orginal userservice GetAllAsync() and repository layer mocked our return user collections.

//Act
var res = await _userService.GetAllAsync();        

after need to verify ther response Type with exepected user count with result of user collection

    //Assert
            Assert.IsType<List<User>>(res);
            Assert.Equal(expectedUserCount, res.Count());         

After running this test class, you can get the test result in test explorer.

Conclusion

We can write unittesting Console project with use of these library needed for running and see the result from explorer.


要查看或添加评论,请登录

jayamoorthi parasuraman的更多文章

社区洞察

其他会员也浏览了