示例#1
0
        /// <summary>
        /// Creates a new <see cref="Delegate"/> instance.
        /// </summary>
        /// <param name="request">The request that describes what to create.</param>
        /// <param name="context">A context that can be used to create other specimens.</param>
        /// <exception cref="ArgumentNullException"><paramref name="context"/> is null.</exception>
        /// <returns>
        /// A new <see cref="Delegate"/> instance, if <paramref name="request"/> is a request for a
        /// <see cref="Delegate"/>; otherwise, a <see cref="NoSpecimen"/> instance.
        /// </returns>
        public object Create(object request, ISpecimenContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var delegateType = request as Type;

            if (delegateType == null)
            {
                return(new NoSpecimen());
            }

            if (!typeof(Delegate).GetTypeInfo().IsAssignableFrom(delegateType))
            {
                return(new NoSpecimen());
            }

            var delegateMethod       = delegateType.GetTypeInfo().GetMethod("Invoke");
            var methodSpecimenParams = DelegateGenerator.CreateMethodSpecimenParameters(delegateMethod);
            var methodSpecimenBody   = DelegateGenerator.CreateMethodSpecimenBody(delegateMethod, context);

            var delegateSpecimen = Expression.Lambda(delegateType, methodSpecimenBody, methodSpecimenParams).Compile();

            return(delegateSpecimen);
        }
示例#2
0
 public override void OneTimeSetup()
 {
     _delegateGeneratorInstanceType    = typeof(DelegateGenerator);
     _delegateGeneratorInstanceFixture = this.Create <DelegateGenerator>(true);
     _delegateGeneratorInstance        = _delegateGeneratorInstanceFixture ?? this.Create <DelegateGenerator>(false);
     CurrentInstance = _delegateGeneratorInstanceFixture;
     ConfigureIgnoringTests(); // Configure ignoring tests.
 }
示例#3
0
        public void AUT_DelegateGenerator_Instantiated_Without_Parameter_No_Throw_Exception_Test()
        {
            // Arrange
            DelegateGenerator instance = null;

            // Act
            var exception = CreateAnalyzer.GetThrownExceptionWhenCreate(out instance);

            // Assert
            instance.ShouldNotBeNull();
            exception.ShouldBeNull();
        }
示例#4
0
        public void AUT_DelegateGenerator_Constructor_Instantiation_With_Parameter_Test()
        {
            // Arrange
            var delegateSpecification           = this.CreateType <IRequestSpecification>();
            DelegateGenerator instance          = null;
            Exception         creationException = null;

            // Act
            Action createAction = () => instance = new DelegateGenerator(delegateSpecification);

            creationException = ActionAnalyzer.GetActionException(createAction);

            // Assert
            instance.ShouldNotBeNull();
            _delegateGeneratorInstance.ShouldNotBeNull();
            _delegateGeneratorInstanceFixture.ShouldNotBeNull();
            Should.NotThrow(createAction);
        }