0

I am having the following method which calls an insert and update methods.

Conditions

  1. In the below ProcessBspLoan function, I am calling other function named getBspData if getBspData returns any result. I will insert the result and update the database as a success for a particular Id

  2. if getBspData throws any Exception I will only update the database as a fail for a particular Id without inserting

Here is the ProcessBspLoan function in the given class

    public class BspLoanProcessor : IBspLoanProcessor
    {
        private readonly IBspClient _bspService;
        private readonly IBspRepository _bspRepository;
        private readonly ILogger<BspLoanProcessor> _logger;
        private readonly IMapper _mapper;
        private readonly IFhaRepository _fhaRepository;

        public BspLoanProcessor(IBspClient bspService, ILogger<BspLoanProcessor> logger, 
            IMapper mapper, IBspRepository bSPRepository, IFhaRepository fhaRepository)
        {
            _bspService = bspService;
            _logger = logger;
            _mapper = mapper;
            _bspRepository = bSPRepository;
            _fhaRepository = fhaRepository;
        }

        public async Task<bool> ProcessBspLoan(BspLoanDetails bspLoanDetails, string transactionId, string triggerType)
        {
            FhaTransactionDetails fhaData = _mapper.Map<FhaTransactionDetails>(bspLoanDetails);

            try
            {
                ////////////////////////////Calling getBspData///////////////////////////////////////
                var result = await _bspService.getBspData(bspLoanDetails.NsmLoanNumber, transactionId);

                result.TransactionId = transactionId;

                await _bspRepository.InsertBspResponse(result);
                
                await _fhaRepository.UpdateFhaTransactionWithLoanNumber(transactionId,"SUCCESS");
                return true;
            }
            catch(Exception ex)
            {
                await _fhaRepository.UpdateFhaTransactionWithLoanNumber(transactionId,"FAIL");
                return false;
            }
        }
    }

I wrote the test method for the above function want to check it is returning true or false based on inputs provided

Here is my test function

    public async Task Test1Async()
    {
        Mock<IBspLoanProcessor> _bspLoanProcessor = new Mock<IBspLoanProcessor>();
        Mock<IBspRepository> _bspRepository = new Mock<IBspRepository>();
        Mock<IFhaRepository> _fhaRepository = new Mock<IFhaRepository>();
        Mock<IBspClient> _bspClient = new Mock<IBspClient>();
        BspLoanDetails bspLoanDetails = new BspLoanDetails
        {
            TriggerType = "BLOB",
            Attempts = 1,
            FirstRunDateTime = DateTime.Now.ToUniversalTime()
        };
        ----> 1
        _bspClient.Setup(x => x.getBspData(It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(new BspResponseDetails()));
        ----> 2
        _bspRepository.Setup(x => x.InsertBspResponse(It.IsAny<BspResponseDetails>())).Returns(Task.CompletedTask);
        ----> 3
        _fhaRepository.Setup(x => x.UpdateFhaTransactionWithLoanNumber(It.IsAny<string>(),It.IsAny<string>())).Returns(Task.CompletedTask);
        bool value = await _bspLoanProcessor.Object.ProcessBspLoan(bspLoanDetails, "123", "SCHEDULE");
        Assert.True(value);
    }

In the above the test, I am checking the first condition

  1. I am returning data as an object whenever anyone called gets data
  2. Inserting BspResponsemethod also I am returning task.completedTask
  3. UpdateFhaTransactionWithLoanNumber also I am returning task.completedTask

The actual expected output is true, but it is returning false.

I am new to Moq and xUnit. Please help me to resolve this issue and also test async and await methods.

IBspClient interface

    public interface IBspClient
    {
        Task<BspResponseDetails> getBspData(string loanNumber,string tid);

        Task<BspHealthCheck> GetHealthStatus();
    }

IBspRepository Interface

    public interface IBspRepository
    {
        Task InsertBspResponse(BspResponseDetails bsp);
    }

IFhaRepository interface

    public interface IFhaRepository
    {
        Task UpdateFhaTransactionWithLoanNumber(string tid, string status);    
    }

Thanks in advance

3
  • What is '_mapper'? I presume it is an 'Automapper' (or something similar) instance defining how to map source to destination. If it is, from standards perspective, should it not be injected in method signature of 'ProcessBspLoan'? I am trying your code but realised that mapper object becomes a dependency that cannot be injected in test from outside. Unless ofcourse you are taking care of it at the class level. Also, can you provide definitions of other dependencies like repository and _bspService class etc? Commented Jul 13, 2020 at 9:49
  • _mapper is an automapper, I am handling classlevel public BspLoanProcessor(IBspClient bspService, ILogger<BspLoanProcessor> logger, IMapper mapper, IBspRepository bSPRepository, IFhaRepository fhaRepository) { _bspService = bspService; _logger = logger; _mapper = mapper; _bspRepository = bSPRepository; _fhaRepository = fhaRepository; } Commented Jul 13, 2020 at 9:56
  • sure I will edit the question and provide each and every interface Commented Jul 13, 2020 at 9:57

1 Answer 1

1

bool value = await _bspLoanProcessor.Object

You're calling a method on your mock. You shouldn't mock the system-under-test, you only mock its dependencies.

Just new the class you want to test, otherwise you will be testing that your mock framework does what you set it up to.

And because you didn't setup ProcessBspLoan(), it returns the default for its return value, so false.

Sign up to request clarification or add additional context in comments.

2 Comments

Ooh okay, I will check once
Thank you knowledge transfer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.