示例#1
0
        public void When_invoking_Build_with_action_generate_new_entity_with_specified_property_values()
        {
            var expectedId          = AnonymousData.Int();
            var expectedRecorded    = AnonymousData.DateTime();
            var expectedDescription = AnonymousData.String();
            var expectedCredit      = AnonymousData.Decimal();
            var expectedDebit       = AnonymousData.Decimal();
            var expectedCategoryId  = AnonymousData.Int();
            var expectedCategory    = new Builder <Category>().Build();

            var entity = new Builder <Transaction>()
                         .Build(t => {
                t.Recorded    = expectedRecorded;
                t.Id          = expectedId;
                t.Description = expectedDescription;
                t.Credit      = expectedCredit;
                t.Debit       = expectedDebit;
                t.CategoryId  = expectedCategoryId;
                t.Category    = expectedCategory;
            });

            Assert.IsNotNull(entity);
            Assert.IsInstanceOfType(entity, typeof(Transaction));
            Assert.AreEqual(expectedId, entity.Id);
            Assert.AreEqual(expectedRecorded, entity.Recorded);
            Assert.AreEqual(expectedDescription, entity.Description);
            Assert.AreEqual(expectedCredit, entity.Credit);
            Assert.AreEqual(expectedDebit, entity.Debit);
            Assert.AreEqual(expectedCategoryId, entity.CategoryId);
            Assert.AreEqual(expectedCategory, entity.Category);
        }
示例#2
0
        protected virtual object GenerateAnonymousData(object entity, Type propertyType, string propertyName, int hierarchyDepth)
        {
            if (propertyType == typeof(string))
            {
                return(AnonymousData.String(propertyName));
            }

            if (propertyType == typeof(sbyte) || propertyType == typeof(byte) || propertyType == typeof(Byte) || propertyType == typeof(SByte))
            {
                return(AnonymousData.Byte());
            }

            if (propertyType == typeof(short) || propertyType == typeof(ushort) || propertyType == typeof(Int16) || propertyType == typeof(UInt16))
            {
                return(AnonymousData.Short());
            }

            if (propertyType == typeof(int) || propertyType == typeof(uint) || propertyType == typeof(Int32) || propertyType == typeof(UInt32))
            {
                return(AnonymousData.Int(3, false));
            }

            if (propertyType == typeof(long) || propertyType == typeof(ulong) || propertyType == typeof(Int64) || propertyType == typeof(UInt64))
            {
                return(AnonymousData.Long());
            }

            if (propertyType == typeof(double) || propertyType == typeof(Double))
            {
                return(AnonymousData.Double());
            }

            if (propertyType == typeof(decimal) || propertyType == typeof(Decimal))
            {
                return(AnonymousData.Decimal());
            }

            if (propertyType == typeof(float) || propertyType == typeof(Single))
            {
                return(AnonymousData.Float());
            }

            if (propertyType == typeof(char) || propertyType == typeof(Char))
            {
                return(AnonymousData.Char());
            }

            if (propertyType == typeof(DateTime))
            {
                return(AnonymousData.DateTime());
            }

            if (propertyType == typeof(TimeSpan))
            {
                return(AnonymousData.TimeSpan());
            }

            if (propertyType.IsValueType)
            {
                return(Activator.CreateInstance(propertyType));
            }

            if (hierarchyDepth > 0 && propertyType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(propertyType))
            {
                var genericTypeArgument = propertyType.GenericTypeArguments.FirstOrDefault();
                if (!genericTypeArgument.IsClass || genericTypeArgument.IsGenericType)
                {
                    return(null);
                }
                var listOfChildEntities = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(genericTypeArgument));

                try
                {
                    for (var i = 1; i <= NumberOfNestedEntitiesInCollections; i++)
                    {
                        var childEntity = GenerateAnonymousDateForChildEntityObject(null, genericTypeArgument, propertyName, hierarchyDepth);
                        listOfChildEntities.Add(childEntity);
                    }
                    return(listOfChildEntities);
                }
                catch (Exception)
                {
                    return((IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(genericTypeArgument)));
                }
            }

            if (hierarchyDepth > 0 && propertyType.IsClass && !propertyType.IsGenericType)
            {
                return(GenerateAnonymousDateForChildEntityObject(entity, propertyType, propertyName, hierarchyDepth));
            }

            return(null);
        }