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); }
public void GetById_invokes_ProcessQueryAsync_on_mediator_passing_GetCategoryByIdQuery_with_correct_data() { var categoryId = AnonymousData.Int(); var response = _controller.Get(categoryId); _mediatorMock.Verify(sm => sm.ProcessQueryAsync(It.Is <GetCategoryByIdQuery>(c => c.CategoryId == categoryId )), Times.Once); }
public void When_invoking_Build_passing_depth_2_Should_generate_default_valid_Transaction_CategoryId_Category_and_Transactions_in_Category() { var expectedNumberOfChildEntitiesInCollection = AnonymousData.Int(1); var builder = new Builder <Category> { NumberOfNestedEntitiesInCollections = expectedNumberOfChildEntitiesInCollection }; var category = builder.Build(1); Assert.IsNotNull(category); Assert.IsNotNull(category.Transactions); Assert.IsNotNull(category.Transactions.Count == expectedNumberOfChildEntitiesInCollection); category.Transactions.ToList().ForEach(t => { Assert.IsNotNull(t); Assert.IsNull(t.Category); Assert.IsTrue(t.CategoryId > 0); Assert.IsTrue(t.Id > 0); Assert.IsTrue(t.Credit > 0); Assert.IsTrue(t.Debit > 0); Assert.IsTrue(!string.IsNullOrWhiteSpace(t.Description)); }); }
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); }