Autofixture can't create instance of PageData

Vote:
 

I'm running into a problem with Autofixture not able to create instances of PageData (or derived types) due to an error:

AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from JczTesting.Models.Pages.LandingPage because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

Request path:
	JczTesting.Models.Pages.LandingPage landingPage
	  JczTesting.Models.Pages.LandingPage

Inner exception messages:
	System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
	  EPiServer.Core.EPiServerException: Property 'PageName' does not exist, can only assign values to existing properties


  ----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> EPiServer.Core.EPiServerException : Property 'PageName' does not exist, can only assign values to existing properties
   at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 81
   at AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\RecursionGuard.cs:line 176
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 77
   at AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\RecursionGuard.cs:line 176
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\Postprocessor.cs:line 272
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 77
   at AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\RecursionGuard.cs:line 176
   at AutoFixture.NUnit3.AutoDataAttribute.Resolve(IParameterInfo parameterInfo) in C:\projects\autofixture\Src\AutoFixture.NUnit3\AutoDataAttribute.cs:line 97
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoFixture.NUnit3.FixedNameTestMethodBuilder.GetParametersForMethod(IMethodInfo method, IEnumerable`1 parameterValues, Int32 autoDataStartIndex) in C:\projects\autofixture\Src\AutoFixture.NUnit3\FixedNameTestMethodBuilder.cs:line 37
--TargetInvocationException
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at AutoFixture.Kernel.AutoPropertiesCommand`1.Execute(Object specimen, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\AutoPropertiesCommand.cs:line 252
   at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\Postprocessor.cs:line 290
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\CompositeSpecimenBuilder.cs:line 52
   at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context) in C:\projects\autofixture\Src\AutoFixture\Kernel\TerminatingWithPathSpecimenBuilder.cs:line 57
--EPiServerException
   at EPiServer.Core.ContentData.SetValue(String index, Object value)

I have created custom specimen builder and hooked it up together with AutoMoqData customization:

public class EpiServerContentTypeSpecimenBuilder : ISpecimenBuilder
    {
        private static readonly string[] OmitProperties = {"PageName"};

        public object Create(object request, ISpecimenContext context)
        {
            var propertyInfo = request as PropertyInfo;
            if (propertyInfo == null) return new NoSpecimen();

            // below line doesn't help at all
            if(OmitProperties.Contains(propertyInfo.Name)) return new NoSpecimen();

            return propertyInfo.IsDefined(typeof(DisplayAttribute)) 
                ? context.Resolve(propertyInfo.PropertyType) 
                : new OmitSpecimen();
        }
    }

And the attribute:

 public class AutoMoqDataAttribute : AutoDataAttribute
    {
        public AutoMoqDataAttribute() : base(() =>
            {
                var f = new Fixture()
                    .Customize(new EpiServerContentTypesCustomization())
                    .Customize(new AutoMoqCustomization { ConfigureMembers = true });
                // below lines doesn't help at all as well
                f.Customize<LandingPage>(composer => composer.Without(pd => pd.PageName));
                f.Customize<PageData>(composer => composer.Without(pd => pd.PageName));
                return f;
            })

        {

        }
    }

And for the sake of completeness, test method:

        [Test, AutoMoqData]
        public void ErrorTest(LandingPage landingPage)
        {
            landingPage.Should().NotBeNull();
        }

Any ideas what the heck? Or how to guide Autofixture to omit certain props?

#223953
Edited, Jun 08, 2020 10:15
Vote:
 

Hi Jerzy, did you try some other library? As far as I remember we use something like FakeMaker for episerver page types.

#225156
Jul 07, 2020 8:24
Vote:
 

Yes, I have incorporated FakeMaker, but also using plain mocks from Moq solves a lot of simple cases.

#225157
Jul 07, 2020 9:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.