Add unit tests for TokensController.cs.

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-11 23:06:35 +03:00
parent e8166dc5f8
commit 1e2b598f7c
3 changed files with 57 additions and 2 deletions

View file

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoFixture.Xunit2;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Retroactiune.Controllers;
using Retroactiune.DataTransferObjects;
using Retroactiune.Models;
using Retroactiune.Services;
using Xunit;
namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers
{
public class TestTokensController
{
[Fact]
public async Task Test_GenerateTokens_InexistentFeedbackReceiver()
{
// Arrange
var feedbackService = new Mock<IFeedbackReceiverService>();
var tokens = new Mock<ITokensService>();
// Test
var controller = new TokensController(feedbackService.Object, tokens.Object);
var result = await controller.GenerateTokens(new GenerateTokensDto());
// Assert
Assert.IsType<BadRequestObjectResult>(result);
}
[Theory, AutoData]
public async Task Test_GenerateTokens_Success(FeedbackReceiver randFedFeedbackReceiver)
{
// Arrange
var feedbackService = new Mock<IFeedbackReceiverService>();
var tokens = new Mock<ITokensService>();
feedbackService.Setup(i => i.FindAsync(It.IsAny<IEnumerable<string>>(),
It.IsAny<int?>(),
It.IsAny<int?>()))
.ReturnsAsync(new[] {randFedFeedbackReceiver});
// Test
var controller = new TokensController(feedbackService.Object, tokens.Object);
var result = await controller.GenerateTokens(new GenerateTokensDto
{
NumberOfTokens = 2,
FeedbackReceiverId = "froid",
});
// Assert
Assert.IsType<OkObjectResult>(result);
tokens.Verify(i => i.GenerateTokensAsync(2, "froid", null), Times.Once);
}
}
}

View file

@ -23,7 +23,7 @@ namespace Retroactiune.Controllers
_tokensService = tokensService; _tokensService = tokensService;
} }
// TODO: Test unit & integrations. // TODO: Test integration.
/// <summary> /// <summary>
/// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client /// Creates a new batch of tokens, the tokens are tied to a FeedbackReceiver and are used by the client
/// when leaving Feedback. /// when leaving Feedback.

View file

@ -13,7 +13,6 @@ namespace Retroactiune.DataAnnotations
/// <returns>True if the date is null or in the future, false otherwise.</returns> /// <returns>True if the date is null or in the future, false otherwise.</returns>
public override bool IsValid(object value) public override bool IsValid(object value)
{ {
// TODO: Test
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var date = value as DateTime?; var date = value as DateTime?;
return !(date <= now); return !(date <= now);