public async Task IShouldCanSeeTheWalletBalance() { var viewModel = new BalanceViewModel { DotzBalance = 10 }; var mockForWalletService = new Mock <IWalletApplicationService>(); mockForWalletService.Setup(d => d.GetBalance(It.IsAny <string>())) .ReturnsAsync(viewModel) .Verifiable(); var mockForHttpContextAccessor = new Mock <IHttpContextAccessor>(); mockForHttpContextAccessor.Setup(d => d.HttpContext.User.Identity.Name) .Returns(It.IsAny <string>()) .Verifiable(); var controller = new WalletController( mockForWalletService.Object, mockForHttpContextAccessor.Object); var result = await controller.Balance(); var resultOk = result as OkObjectResult; var resultData = resultOk.Value as BalanceViewModel; Assert.IsInstanceOfType(result, typeof(ActionResult)); Assert.AreEqual(resultOk.StatusCode, 200); Assert.AreEqual(resultData.DotzBalance, viewModel.DotzBalance); mockForWalletService.VerifyAll(); }
public async Task Balance_GetBalanceIs4_ReturnsOkAndBalance4() { //// Arrange decimal currentBalanceAmount = 4; Balance currentBalance = new Balance() { Amount = currentBalanceAmount }; BalanceResponse expectedBalanceResponse = new BalanceResponse() { Amount = currentBalanceAmount }; // Setup Mocks Mock <ILogger <WalletController> > loggerMock = new Mock <ILogger <WalletController> >(); ILogger <WalletController> logger = loggerMock.Object; Mock <IMapper> mapperMock = new Mock <IMapper>(); mapperMock .Setup(mapper => mapper.Map <BalanceResponse>(currentBalance)) .Returns(expectedBalanceResponse); IMapper mapper = mapperMock.Object; Mock <IWalletService> walletServiceMock = new Mock <IWalletService>(); walletServiceMock .Setup(walletService => walletService.GetBalanceAsync()) .Returns(Task.FromResult(currentBalance)); IWalletService walletService = walletServiceMock.Object; // Initialize SUT WalletController walletController = new WalletController(logger, mapper, walletService); //// Act ActionResult <BalanceResponse> actionResult = await walletController.Balance(); ActionResult actualActionResult = actionResult.Result; /// Assert OkObjectResult okObjectResult = Assert.IsType <OkObjectResult>(actionResult.Result); BalanceResponse actualBalanceResponse = Assert.IsType <BalanceResponse>(okObjectResult.Value); Assert.Equal(expectedBalanceResponse, actualBalanceResponse); walletServiceMock.Verify(walletService => walletService.GetBalanceAsync(), Times.Once); walletServiceMock.VerifyNoOtherCalls(); }