是否测试Automapper返回集合

时间:2019-05-07 03:06:08

标签: unit-testing asp.net-core automapper

我正在测试我的一项服务-movieService。 GetLatestMovies方法应返回所有按日期排序的电影。我正在使用自动映射器将实体Movie映射到MovieViewModel。

*问题1:* 我应该如何测试,应该在测试中设置return集合还是什么?

*问题2:* 我正在用一些电影填充InMemory数据库,并且我希望movieService能够提供正确的排序结果,我应该如何检查,如果服务返回的是正确的结果,如果我设置了自动映射器的返回值?

TestUtils.FillContextWithActorsMoviesAndGenres(options)-仅用几部电影填充上下文。

这是我正在测试的movieService方法

public async Task<ICollection<MovieViewModel>> GetLatestMoviesAsync()
        {
            var movies = await this.context.Movies
                .Include(um => um.ApplicationUserMovie)
                    .ThenInclude(u => u.User)
                .Include(x => x.Genre)
                .Include(x => x.MovieActor)
                    .ThenInclude(x => x.Actor)
                .OrderByDescending(x => x.CreatedOn).ToListAsync();

            var returnMovies = this.mappingProvider.MapTo<ICollection<MovieViewModel>>(movies);

            return returnMovies;
        }
[TestMethod]
        public async Task Return_TwoMoviesWithHighestRating()
        {
            var dabataseName = nameof(Return_TwoMoviesWithHighestRating);

            var options = TestUtils.GetOptions(dabataseName);

            // We fill the context with data and save it.
            TestUtils.FillContextWithActorsMoviesAndGenres(options);


            var movieOne = new MovieViewModel()
            {
                Name = "BestRatedMovieTest",
                Duration = 90,
                Director = "TestDirector",
                Storyline = "TestStoryline",
                ImageUrl = "TestImageUrl",
                Genre = "Comedy"
            };

            var movieTwo = new MovieViewModel()
            {
                Name = "SecondMovieTestName",
                Duration = 90,
                Director = "TestDirector",
                Storyline = "TestStoryline",
                ImageUrl = "TestImageUrl",
                Genre = "Comedy"
            };

            var collectionMovieViewModels = new List<MovieViewModel>() { movieOne, movieTwo };


            var mappingProviderMock = new Mock<IMappingProvider>();
            mappingProviderMock
                .Setup(x => x.MapTo<ICollection<MovieViewModel>>(It.IsAny<List<Movie>>()))
                .Returns(collectionMovieViewModels);


            using (var actAndAssertContext = new MovieManagementContext(options))
            {
                var sut = new MovieService(actAndAssertContext, mappingProviderMock.Object);
                var movies = await sut.GetLatestMoviesAsync();

                Assert.AreEqual(2, movies.Count());
                Assert.AreEqual("BestRatedMovieTest", movies.FirstOrDefault().Name);
            }
        }

1 个答案:

答案 0 :(得分:0)

我创建了一个空集合,并设置了该方法的回调以填充该集合,

var collectionOfMovies = new List<Movie>();

var mappingProviderMock = new Mock<IMappingProvider>();
            mappingProviderMock
                .Setup(x => x.MapTo<ICollection<MovieViewModel>>(It.IsAny<List<Movie>>()))
                .Callback<object>(inputargs => collectionOfMovies = inputargs as List<Movie>);