diff --git a/src/StubGenerator.Test.Models/EnTestEnum.cs b/src/StubGenerator.Test.Models/EnTestEnum.cs new file mode 100644 index 0000000..28ba329 --- /dev/null +++ b/src/StubGenerator.Test.Models/EnTestEnum.cs @@ -0,0 +1,9 @@ +namespace StubGenerator.Test.Models +{ + public enum EnTestEnum + { + Option1, + Option2, + Option3 + } +} diff --git a/src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs b/src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs index 54a921a..baba616 100644 --- a/src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs +++ b/src/StubGenerator.Test.Models/ModelWithComplexTypeProperty.cs @@ -18,6 +18,10 @@ public class ModelWithComplexTypeProperty public InnerComplexType ComplexType { get; set; } + public ModelConstructorHasParameters ModelConstructorHasParameters { get; set; } + + public ModelConstructorMixed ModelConstructorMixed { get; set; } + public List CollectionTypeComplex { get; set; } } @@ -42,10 +46,29 @@ public class InnerComplexType public Nullable NullableIntegerProperty2 { get; set; } } - public enum EnTestEnum + + public class ModelConstructorHasParameters + { + public ModelConstructorHasParameters(int intField) + { + IntField = intField; + } + + public int IntField { get; set; } + } + + + public class ModelConstructorMixed { - Option1, - Option2, - Option3 + public ModelConstructorMixed() + { + } + + public ModelConstructorMixed(string firstName) + { + FirstName = firstName; + } + + public string FirstName { get; set; } } } diff --git a/src/StubGenerator.Test/StubManagerTests.cs b/src/StubGenerator.Test/StubManagerTests.cs index d619aae..d782287 100644 --- a/src/StubGenerator.Test/StubManagerTests.cs +++ b/src/StubGenerator.Test/StubManagerTests.cs @@ -146,5 +146,14 @@ public void Should_Set_Given_Values_While_Generating_Data() Assert.Equal(givenEnum, generatedStubData.EnumProperty); Assert.Equal(givenInteger, generatedStubData.IntegerProperty); } + + [Fact(DisplayName = "Should Generate Data For Complex Type Properties Check Parameterless")] + public void Should_Generate_Data_For_Complex_Type_Properties_Check_Parameterless() + { + var generatedStubData = _stubManager.CreateNew(); + Assert.NotNull(generatedStubData.ComplexType); + Assert.NotNull(generatedStubData.ModelConstructorMixed); + Assert.Null(generatedStubData.ModelConstructorHasParameters); + } } } diff --git a/src/StubMiddleware.Core/Core/StubManager.cs b/src/StubMiddleware.Core/Core/StubManager.cs index 62c315a..181974e 100644 --- a/src/StubMiddleware.Core/Core/StubManager.cs +++ b/src/StubMiddleware.Core/Core/StubManager.cs @@ -13,7 +13,7 @@ public class StubManager : IStubManager { private readonly IFakeDataFactory _fakeDataFactory; private readonly IStubTypeCache _stubTypeCache; - + public StubManager(StubManagerOptions stubManagerOptions) : this(stubManagerOptions, new MemoryStubTypeCache(), new FakeDataFactory()) { @@ -88,9 +88,13 @@ private void FillPropertiesWithFakeData(TObject obj, PropertyInfo[] pro { if (!property.PropertyType.IsSimple()) { - dynamic innerComplexObj = Activator.CreateInstance(property.PropertyType); - obj.GetType().GetProperty(property.Name).SetValue(obj, innerComplexObj); - FillPropertiesWithFakeData(innerComplexObj, _stubTypeCache.GetOrAdd(innerComplexObj, innerComplexObj.GetType().GetProperties())); + /*check the type has parameterless constructor*/ + if ((property.PropertyType.GetConstructor(Type.EmptyTypes) != null)) + { + dynamic innerComplexObj = Activator.CreateInstance(property.PropertyType); + obj.GetType().GetProperty(property.Name).SetValue(obj, innerComplexObj); + FillPropertiesWithFakeData(innerComplexObj, _stubTypeCache.GetOrAdd(innerComplexObj, innerComplexObj.GetType().GetProperties())); + } } else {