public void InvokeMethod_Should_PerformTheExpectedCallsAndReturnFalse_Given_TheInvokedMethodReturnsAAssertionResultRetryObject()
        {
            GameObject gameObject = UnitTestUtility.CreateGameObject();

            ExtensionRunnerBusinessLogic businessLogic = new ExtensionRunnerBusinessLogic(gameObject);

            Component        component                   = Substitute.For <Component>();
            MethodInfo       mockedMethodInfo            = Substitute.For <MethodInfo>();
            string           errorText                   = "message";
            IAssertionResult methodInvokeAssertionResult = new AssertionResultRetry(errorText);

            object[] parameters = new object[0];
            mockedMethodInfo.Invoke(component, parameters).Returns(methodInvokeAssertionResult);
            FullMethodDescription methodDescription = Substitute.For <FullMethodDescription>();

            methodDescription.Method = mockedMethodInfo;
            methodDescription.GetFullName().Returns <string>("component.method");
            ExtensionRunnerBusinessLogic mockedBusinessLogic = Substitute.For <ExtensionRunnerBusinessLogic>(gameObject);

            DateTime firstNowDatetime = new DateTime(2017, 05, 01, 00, 00, 00, 000);

            mockedBusinessLogic.StartDelayTime = firstNowDatetime;
            DateTime secondNowDatetime = new DateTime(2017, 05, 01, 00, 00, 00, 000);

            mockedBusinessLogic.DateTimeNow().Returns <DateTime>(secondNowDatetime);

            mockedBusinessLogic.GetParametersValues(methodDescription).Returns <object[]>(parameters);
            string scenarioText                  = "scenarioText";
            string bddMethodLocation             = "bddMethodLocation";
            List <FullMethodDescription> methods = new List <FullMethodDescription>();

            methods.Add(methodDescription);

            mockedBusinessLogic.MethodsDescription = methods;
            mockedBusinessLogic.GetScenarioTextForErrorInSpecificMethod(methods, methodDescription).Returns(scenarioText);
            mockedBusinessLogic.GetbddMethodLocationForSpecificMethod(methods, methodDescription).Returns(bddMethodLocation);

            bool result = businessLogic.InvokeMethod(mockedBusinessLogic, methodDescription, gameObject);

            Received.InOrder(() =>
            {
                mockedBusinessLogic.DateTimeNow();
                mockedBusinessLogic.DateTimeNow();
                mockedBusinessLogic.GetParametersValues(methodDescription);
                mockedMethodInfo.Invoke(component, parameters);
                mockedBusinessLogic.DateTimeNow();

                mockedBusinessLogic.InvokeAssertionFailed(errorText, scenarioText, bddMethodLocation, gameObject);
            });
            Assert.IsFalse(result, "The method InvokeMethod doesn't return the right state");
        }
Beispiel #2
0
        /// <summary>
        /// Invokes the <paramref name="methodDescription"/> method.
        /// </summary>
        /// <param name="businessLogic">The business logic.</param>
        /// <param name="methodDescription">The method description.</param>
        /// <param name="gameObject">The game object.</param>
        /// <returns>True if the method is executed or false if the method still has to be executed.</returns>
        public virtual bool InvokeMethod(ExtensionRunnerBusinessLogic businessLogic, FullMethodDescription methodDescription, GameObject gameObject)
        {
            bool performed = false;

            if (businessLogic.DateTimeNow().Subtract(businessLogic.StartDelayTime).TotalMilliseconds >= methodDescription.Delay)
            {
                if (businessLogic.StartTimoutTime == null)
                {
                    businessLogic.StartTimoutTime = businessLogic.DateTimeNow();
                }

                if (methodDescription.Method != null && !methodDescription.Method.Equals(string.Empty))
                {
                    MethodInfo       method          = methodDescription.Method;
                    Component        component       = methodDescription.ComponentObject;
                    object[]         parameters      = businessLogic.GetParametersValues(methodDescription);
                    IAssertionResult executionResult = null;

                    object executionResultObject = method.Invoke(component, parameters);
                    if (executionResultObject == null)
                    {
                        string errorText         = "The Step Method return null.";
                        string scenarioText      = businessLogic.GetScenarioTextForErrorInSpecificMethod(businessLogic.MethodsDescription, methodDescription);
                        string bddMethodLocation = businessLogic.GetbddMethodLocationForSpecificMethod(businessLogic.MethodsDescription, methodDescription);
                        businessLogic.InvokeAssertionFailed(errorText, scenarioText, bddMethodLocation, gameObject);
                        return(true);
                    }

                    if (typeof(AssertionResultSuccessful).IsAssignableFrom(executionResultObject.GetType()) ||
                        typeof(AssertionResultFailed).IsAssignableFrom(executionResultObject.GetType()) ||
                        typeof(AssertionResultRetry).IsAssignableFrom(executionResultObject.GetType()))
                    {
                        executionResult = (IAssertionResult)executionResultObject;
                    }
                    else
                    {
                        string errorText         = "The return value of the Step Method is not a valid IAssertionResult implementation.";
                        string scenarioText      = businessLogic.GetScenarioTextForErrorInSpecificMethod(businessLogic.MethodsDescription, methodDescription);
                        string bddMethodLocation = businessLogic.GetbddMethodLocationForSpecificMethod(businessLogic.MethodsDescription, methodDescription);
                        businessLogic.InvokeAssertionFailed(errorText, scenarioText, bddMethodLocation, gameObject);
                        return(true);
                    }

                    if (executionResult is AssertionResultSuccessful)
                    {
                        performed = true;
                    }
                    else if (executionResult is AssertionResultFailed)
                    {
                        string errorText = ((AssertionResultFailed)executionResult).Text;

                        string scenarioText      = businessLogic.GetScenarioTextForErrorInSpecificMethod(businessLogic.MethodsDescription, methodDescription);
                        string bddMethodLocation = businessLogic.GetbddMethodLocationForSpecificMethod(businessLogic.MethodsDescription, methodDescription);

                        businessLogic.InvokeAssertionFailed(errorText, scenarioText, bddMethodLocation, gameObject);
                        performed = true;
                    }
                    else if (executionResult is AssertionResultRetry)
                    {
                        if (businessLogic.DateTimeNow().Subtract(businessLogic.StartTimoutTime ?? DateTime.MaxValue).TotalMilliseconds >= methodDescription.TimeOut)
                        {
                            string errorText = ((AssertionResultRetry)executionResult).Text;

                            string scenarioText      = businessLogic.GetScenarioTextForErrorInSpecificMethod(businessLogic.MethodsDescription, methodDescription);
                            string bddMethodLocation = businessLogic.GetbddMethodLocationForSpecificMethod(businessLogic.MethodsDescription, methodDescription);

                            businessLogic.InvokeAssertionFailed(errorText, scenarioText, bddMethodLocation, gameObject);

                            performed = true;
                        }

                        performed = false;
                    }
                }
                else
                {
                    performed = true;
                    businessLogic.StartTimoutTime = null;
                }
            }

            return(performed);
        }