如何模拟具有Func和Generic参数的方法

时间:2019-10-06 21:26:38

标签: c# moq

如何使用C#Moq模拟这样的方法?

List<T> GetMoviesWithCache<T>(IEnumerable<string> pids, PlayType? playType, Func<List<string>, PlayType?, bool?, IDeviceType, List<T>> getMoviesToCache, bool? requiresPin, bool? returnCompatibleMedias = null, IDeviceType deviceType = null, bool throwExceptionWhenNoMoviesWereFound = true) where T : class;

这里的要点是该方法具有一个具有列表的Func作为参数。

我尝试过:

            var moviesFound = new List<Model.ISimpleMovie>
            {
                new Model.SimpleMovie { ID = 1 },
                new Model.SimpleMovie { ID = 3 },
                new Model.SimpleMovie { ID = 5 },
                new Model.SimpleMovie { ID = 7 }
            };

            MovieServiceMock
                .Setup(_ => _.GetMoviesWithCache(
                    It.IsAny<IEnumerable<string>>(),
                    It.IsAny<PlayType?>(),
                    It.IsAny<Func<List<string>, PlayType?, bool?, IDeviceType, List<Model.ISimpleMovie>>>(),
                    It.IsAny<bool?>(),
                    It.IsAny<bool?>(),
                    It.IsAny<IDeviceType>(),
                    It.IsAny<bool>()))
                .Returns(moviesFound);

因为运行此方法时,在我的测试中,T证明是ISimpleMovie。

但是,它不起作用,没有编译器错误或警告出现。

0 个答案:

没有答案