Last active
March 24, 2017 06:37
-
-
Save marcduiker/d92aa66366c27459bd81cdf162aa0b7a to your computer and use it in GitHub Desktop.
Unit test with a lean arrange section. The unit test uses private methods to create the required fake instances using AutoFixture and FakeItEasy. Note that named arguments are used to clarify the meaning of the arguments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void GetHighestRatedMovies_RepositoryContainsMoviesWithRatings_ReturnsMoviesOrderedByDescendingRating() | |
{ | |
// Arrange | |
IMovieRepository fakeMovieRepository = GetFakeMovieRepository(nrOfMoviesInRepository: 20); | |
IMovieService movieService = new MovieService(fakeMovieRepository, context: null, logger: null); | |
MovieServiceRequest request = GetMovieServiceRequest(nrOfMoviesToReturn: 5); | |
// Act | |
IList<Movie> movies = movieService.GetHighestRatedMovies(request); | |
// Assert | |
movies.Should().BeInDescendingOrder(movie => movie.Rating); | |
} | |
private static IMovieRepository GetFakeMovieRepository(int nrOfMoviesInRepository) | |
{ | |
var fixture = new Fixture(); | |
var movies = fixture.CreateMany<Movie>(nrOfMoviesInRepository).ToList(); | |
var fakeRepository = A.Fake<IMovieRepository>(); | |
A.CallTo(() => fakeRepository.GetAll()).Returns(movies); | |
return fakeRepository; | |
} | |
private static MovieServiceRequest GetMovieServiceRequest(int nrOfMoviesToReturn) | |
{ | |
return new MovieServiceRequest | |
{ | |
NumberOfMoviesToReturn = nrOfMoviesToReturn | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment