forked from Guestlogix/back-end-take-home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTakeHomeControllerTests.cs
102 lines (88 loc) · 3.75 KB
/
TakeHomeControllerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using TakeHome.Mediator.Requests;
using TakeHome.Mediator.Responses;
using TakeHome.Services;
using TakeHome.Web.Api.Controllers;
using Xunit;
namespace TakeHome.Web.Api.Tests
{
public class TakeHomeControllerTests
{
private readonly Mock<IMediator> _mediator;
private TakeHomeController sut;
public TakeHomeControllerTests()
{
_mediator = new Mock<IMediator>();
sut = new TakeHomeController(_mediator.Object);
}
[Fact]
public async void Should_ReturnOk_When_ShortestRouteIsFound()
{
//Arrange
_mediator.Setup(s =>
s.Send(It.IsAny<GetShortestRouteRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetShortestRouteResponse(true, false, ""));
//Act
var result = await sut.Get(new GetRequest { Origin = It.IsAny<string>(), Destination = It.IsAny<string>() });
var okResult = result as ObjectResult;
//Assert
Assert.NotNull(okResult);
Assert.True(okResult is OkObjectResult);
Assert.IsType<string>(okResult.Value);
Assert.Equal(StatusCodes.Status200OK, okResult.StatusCode);
}
[Fact]
public async void Should_ReturnNotFound_When_ShortestRouteIsNotFound()
{
_mediator.Setup(s =>
s.Send(It.IsAny<GetShortestRouteRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetShortestRouteResponse(true, true, ""));
//Act
var result = await sut.Get(new GetRequest { Origin = It.IsAny<string>(), Destination = It.IsAny<string>() });
var notFoundResult = result as ObjectResult;
//Assert
Assert.NotNull(notFoundResult);
Assert.True(notFoundResult is NotFoundObjectResult);
Assert.IsType<string>(notFoundResult.Value);
Assert.Equal(StatusCodes.Status404NotFound, notFoundResult.StatusCode);
}
[Fact]
public async void Should_ReturnBadRequest_When_RequestHasInvalidParameters()
{
//Arrange
_mediator.Setup(s =>
s.Send(It.IsAny<GetShortestRouteRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetShortestRouteResponse(false, false, ""));
//Act
var result = await sut.Get(new GetRequest { Origin = It.IsAny<string>(), Destination = It.IsAny<string>() });
var badRequestResult = result as ObjectResult;
//Assert
Assert.NotNull(badRequestResult);
Assert.True(badRequestResult is BadRequestObjectResult);
Assert.IsType<string>(badRequestResult.Value);
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
}
[Fact]
public async void Should_ReturnInternalServerError_When_RequestThrowsException()
{
//Arrange
_mediator.Setup(s =>
s.Send(It.IsAny<GetShortestRouteRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetShortestRouteResponse(" "));
//Act
var result = await sut.Get(new GetRequest { Origin = It.IsAny<string>(), Destination = It.IsAny<string>() });
var errorResult = result as ObjectResult;
//Assert
Assert.NotNull(errorResult);
Assert.IsType<string>(errorResult.Value);
Assert.Equal(StatusCodes.Status500InternalServerError, errorResult.StatusCode);
}
}
}