I am having the following method which calls an insert and update methods.
Conditions
In the below
ProcessBspLoanfunction, I am calling other function namedgetBspDataifgetBspDatareturns any result. I will insert the result and update the database as a success for a particular Idif
getBspDatathrows 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
- I am returning data as an object whenever anyone called gets data
- Inserting
BspResponsemethodalso I am returningtask.completedTask UpdateFhaTransactionWithLoanNumberalso I am returningtask.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