27
loading...
This website collects cookies to deliver better user experience
[ApiController]
[Route("api/v1/[controller]")]
public class MoviesController : ControllerBase
{
private readonly IFilmSpecialist _filmSpecialist;
public MoviesController(IFilmSpecialist filmSpecialist)
{
_filmSpecialist = filmSpecialist;
}
[HttpGet]
public Movie Get()
{
Log.Information("Let me ask the film specialist...");
var movie = _filmSpecialist.SuggestSomeMovie();
Log.Information("Suggested movie: {Movie}", movie);
return movie;
}
}
public class FilmSpecialist : IFilmSpecialist
{
private static readonly Movie[] Films =
{
new("RoboCop", "10/08/1987", new[] {"Action", "Thriller", "Science Fiction"}, "1h 42m"),
new("The Matrix", "05/21/1999", new[] {"Action", "Science Fiction"}, "2h 16m"),
new("Soul", "12/25/2020", new[] {"Family", "Animation", "Comedy", "Drama", "Music", "Fantasy"}, "1h 41m"),
new("Space Jam", "12/25/1996", new[] {"Adventure", "Animation", "Comedy", "Family"}, "1h 28m"),
new("Aladdin", "07/03/1993", new[] {"Animation", "Family", "Adventure", "Fantasy", "Romance"}, "1h 28m"),
new("The World of Dragon Ball Z", "01/21/2000", new[] {"Action"}, "20m"),
};
public Movie SuggestSomeMovie()
{
Log.Debug("OKAY! Which film will I suggest 🤔");
Random random = new();
var filmIndexThatIWillSuggest = random.Next(0, Films.Length);
Log.Information("Will suggest the film with index {FilmIndex}!", filmIndexThatIWillSuggest);
return Films[filmIndexThatIWillSuggest];
}
}
public class FilmSpecialistTests
{
private readonly IFilmSpecialist _filmSpecialist = new FilmSpecialist();
[Fact]
public void ShouldReturnRandomMovieWhenAsked()
{
// Act
var suggestedMovie = _filmSpecialist.SuggestSomeMovie();
// Assert
var expectedTiles = new[]
{
"RoboCop", "The Matrix", "Soul", "Space Jam", "Aladdin", "The World of Dragon Ball Z"
};
suggestedMovie.Title.Should().BeOneOf(expectedTiles);
}
}
ConfigureTestServices
. To illustrate a complete example:public class MoviesControllerITests : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly IFilmSpecialist _filmSpecialist;
private HttpClient _httpClient;
public MoviesControllerITests(WebApplicationFactory<Startup> factory)
{
_filmSpecialist = Mock.Of<IFilmSpecialist>();
_httpClient = factory.WithWebHostBuilder(builder =>
{
// https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-5.0#inject-mock-services
builder.ConfigureTestServices(services =>
{
services.RemoveAll<IFilmSpecialist>();
services.TryAddTransient(_ => _filmSpecialist);
});
}).CreateClient();
}
[Fact]
public async Task ShouldCreateGameGivenFirstMovementIsBeingExecuted()
{
// Arrange
var requestPath = "/api/v1/movies";
var movieToBeSuggested = new Movie("Schindler's List", "12/31/1993", new[] {"Drama", "History", "War"}, "3h 15m");
Mock.Get(_filmSpecialist)
.Setup(f => f.SuggestSomeMovie())
.Returns(movieToBeSuggested)
.Verifiable();
// Act
var response = await _httpClient.GetAsync(requestPath);
response.StatusCode.Should().Be(HttpStatusCode.OK);
var movie = await response.Content.ReadFromJsonAsync<Movie>();
// Assert
movie.Should().BeEquivalentTo(movieToBeSuggested);
Mock.Get(_filmSpecialist).Verify();
}
}
WebApplicationFactory
, you must install the package:Microsoft.AspNetCore.Mvc.Testing