public static object Create(Type resultCollectionType, MocksRepository repo, IMockReplicator replicator, IEnumerable collection)
		{
			if (resultCollectionType == typeof(string))
				return null;

			Type sourceType = collection.GetType();
			if (resultCollectionType.IsAssignableFrom(sourceType))
				return collection;

			var enumerableType = resultCollectionType.GetImplementationOfGenericInterface(typeof(IEnumerable<>)) ?? typeof(IEnumerable);
			if (!enumerableType.IsAssignableFrom(resultCollectionType))
				throw new MockException("Return value is not an enumerable type.");

			var elementType = enumerableType.IsGenericType ? enumerableType.GetGenericArguments()[0] : typeof(object);

			var ilistType = typeof(IList<>).MakeGenericType(elementType);
			var iqueryableType = typeof(IQueryable<>).MakeGenericType(elementType);

			IEnumerable list;
			if (typeof(ICollection).IsAssignableFrom(sourceType))
			{
				list = collection;
			}
			else
			{
				var listType = typeof(List<>).MakeGenericType(elementType);
				var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(elementType);

				var castCollection = castMethod.Invoke(null, new[] { collection });
				list = (IEnumerable)MockingUtil.CreateInstance(listType, castCollection);
			}

			var listBehavior = new DelegatedImplementationBehavior(list,
				new[]
				{
					ilistType,
					typeof(IList),
				});

			var queryable = list.AsQueryable();
			var queryableType = queryable.GetType();
			var queryableBehavior = new DelegatedImplementationBehavior(queryable,
				new[] { queryableType.GetImplementationOfGenericInterface(typeof(IQueryable<>)) });

			if (replicator != null)
			{
				var mock = replicator.CreateSimilarMock(repo, resultCollectionType, null, true, null);
				var mockMixin = MocksRepository.GetMockMixin(mock, null);
				mockMixin.FallbackBehaviors.Insert(0, queryableBehavior);
				mockMixin.FallbackBehaviors.Insert(0, listBehavior);
				return mock;
			}
			else
			{
				return repo.Create(resultCollectionType, null, Behavior.Loose, MockingUtil.EmptyTypes, null,
					null, null, new List<IBehavior> { listBehavior, queryableBehavior });
			}
		}
Beispiel #2
0
        public static object Create(Type resultCollectionType, MocksRepository repo, IMockReplicator replicator, IEnumerable collection)
        {
            if (resultCollectionType == typeof(string))
            {
                return(null);
            }

            Type sourceType = collection.GetType();

            if (resultCollectionType.IsAssignableFrom(sourceType))
            {
                return(collection);
            }

            var enumerableType = resultCollectionType.GetImplementationOfGenericInterface(typeof(IEnumerable <>)) ?? typeof(IEnumerable);

            if (!enumerableType.IsAssignableFrom(resultCollectionType))
            {
                throw new MockException("Return value is not an enumerable type.");
            }

            var elementType = enumerableType.IsGenericType ? enumerableType.GetGenericArguments()[0] : typeof(object);

            var ilistType      = typeof(IList <>).MakeGenericType(elementType);
            var iqueryableType = typeof(IQueryable <>).MakeGenericType(elementType);

            IEnumerable list;

            if (typeof(ICollection).IsAssignableFrom(sourceType))
            {
                list = collection;
            }
            else
            {
                var listType   = typeof(List <>).MakeGenericType(elementType);
                var castMethod = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(elementType);

                var castCollection = castMethod.Invoke(null, new[] { collection });
                list = (IEnumerable)MockingUtil.CreateInstance(listType, castCollection);
            }

            var listBehavior = new DelegatedImplementationBehavior(list,
                                                                   new[]
            {
                ilistType,
                typeof(IList),
            });

            var queryable         = list.AsQueryable();
            var queryableType     = queryable.GetType();
            var queryableBehavior = new DelegatedImplementationBehavior(queryable,
                                                                        new[] { queryableType.GetImplementationOfGenericInterface(typeof(IQueryable <>)) });

            if (replicator != null)
            {
                var mock      = replicator.CreateSimilarMock(repo, resultCollectionType, null, true, null);
                var mockMixin = MocksRepository.GetMockMixin(mock, null);
                mockMixin.FallbackBehaviors.Insert(0, queryableBehavior);
                mockMixin.FallbackBehaviors.Insert(0, listBehavior);
                return(mock);
            }
            else
            {
                return(repo.Create(resultCollectionType, null, Behavior.Loose, MockingUtil.EmptyTypes, null,
                                   null, null, new List <IBehavior> {
                    listBehavior, queryableBehavior
                }));
            }
        }