예제 #1
0
        /// <summary>
        /// Validate that if a specified argument value is null, that a specified exception is thrown on an Async method
        /// </summary>
        /// <typeparam name="T">Type under test</typeparam>
        /// <typeparam name="TException">Exception expected to be thrown</typeparam>
        /// <param name="methodName">Method to be test</param>
        /// <param name="parameterName">Method Parameter Name to verify</param>
        /// <param name="parameterValue">Parameter Value (Default null)</param>
        public static void ValidateExceptionIsThrownIfParameterIsNullAsync <T, TException>(string methodName, string parameterName, object parameterValue = null)
            where TException : Exception
        {
            if (methodName == null)
            {
                throw new ArgumentNullException(nameof(methodName));
            }
            if (parameterName == null)
            {
                throw new ArgumentNullException(nameof(parameterName));
            }

            var methodInfo            = GetMethodInformation <T>(methodName, parameterName);
            var methodParameters      = methodInfo.GetParameters();
            var methodParameterValues = new List <object>();

            foreach (var currentParameter in methodParameters)
            {
                if (!string.IsNullOrWhiteSpace(parameterName) && currentParameter.Name == parameterName)
                {
                    methodParameterValues.Add(parameterValue);
                    continue;
                }

                methodParameterValues.Add(currentParameter.CreateRandomValue());
            }

            var constructedObject     = ConstructorTestHelper.ConstructObject(typeof(T));
            var argumentNullException = Assert.ThrowsAsync(typeof(TException),
                                                           () => (Task)methodInfo.Invoke(constructedObject, methodParameterValues.ToArray()),
                                                           $"{typeof(TException).Namespace} Exception not throw for Method [{methodName}] Parameter [{parameterName}] on {typeof(T).FullName}");

            argumentNullException.Message.Should().Contain(parameterName);
        }
예제 #2
0
        /// <summary>
        /// Validate when setting a property, the value is actually available via get
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        public static void ValidateGetAndSet <T>(string propertyName)
            where T : class
        {
            var objectUnderTest = ConstructorTestHelper.ConstructObject <T>();

            if (objectUnderTest == null)
            {
                Assert.Fail($"Failed to create {typeof(T).FullName} to test Property Get and Set for {propertyName}");
            }

            objectUnderTest.ValidateGetAndSet(propertyName);
        }
예제 #3
0
        /// <summary>
        /// Validate that if a specified argument value is null, that a ArgumentNullException is thrown
        /// </summary>
        /// <typeparam name="T">Type under test</typeparam>
        /// <param name="methodName">Method to be test</param>
        /// <param name="parameterName">Method Parameter Name to verify</param>
        /// <param name="parameterValue">Parameter Value (Default null)</param>
        public static void ValidateArgumentNullExceptionIfParameterIsNull <T>(string methodName, string parameterName, object parameterValue = null)
        {
            if (methodName == null)
            {
                throw new ArgumentNullException(nameof(methodName));
            }
            if (parameterName == null)
            {
                throw new ArgumentNullException(nameof(parameterName));
            }

            var methodInfo            = GetMethodInformation <T>(methodName, parameterName);
            var methodParameters      = methodInfo.GetParameters();
            var methodParameterValues = new List <object>();

            foreach (var currentParameter in methodParameters)
            {
                if (!string.IsNullOrWhiteSpace(parameterName) && currentParameter.Name == parameterName)
                {
                    methodParameterValues.Add(parameterValue);
                    continue;
                }

                methodParameterValues.Add(currentParameter.CreateRandomValue());
            }

            var constructedObject   = ConstructorTestHelper.ConstructObject(typeof(T));
            var invocationException = Assert.Throws <TargetInvocationException>(() => methodInfo.Invoke(constructedObject, methodParameterValues.ToArray()));

            var argumentNullException = (invocationException.InnerException as ArgumentNullException);

            if (argumentNullException == null)
            {
                Assert.Fail($"ArgumentNullException not throw for Method [{methodName}] Parameter [{parameterName}] on {typeof(T).FullName}");
            }

            argumentNullException.ParamName.Should().Be(parameterName);
        }