Exemplo n.º 1
0
        /// <summary>
        /// Handles the exceptions thrown by the testing method.
        /// </summary>
        private void HandleRunnerException(Exception ex, CSpecTestItem testItem, ProxyTrace trace)
        {
            Failed++;

            if (ex.InnerException != null)
            {
                testItem.Results = "Test Failed: " + ex.InnerException.Message;
            }
            else
            {
                testItem.Results = "CSpec Exception: " + ex.Message;
            }

            if (trace != null)
            {
                StringBuilder traceErrorBuilder = new StringBuilder();
                traceErrorBuilder.AppendLine("\n Trace: ");

                //foreach (var item in trace.MethodCalls)
                //    traceErrorBuilder.AppendLine(item);

                testItem.Results += traceErrorBuilder.ToString();
            }

            testItem.TestSucceed = false;

            if (AfterOperation != null)
            {
                AfterOperation(testItem);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Used on methods to instrut that a certain set of parameter data
        /// should not throw a specified exception.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="N"></typeparam>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T NotRaise <T, N>(this T obj, N value)
        {
            ProxyTrace  trace      = GetTrace();
            ProxyMethod methodCall = trace.MethodCalls.Last();

            if (methodCall.Ex == null)
            {
                return(obj);
            }

            throw new CSpecException(ExceptionMessages.Key + " @not_raise");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the Trace object if the
        /// consumer implement an InterfaceType
        /// </summary>
        /// <returns></returns>
        private static ProxyTrace GetTrace()
        {
            object lookupObj = Testing.CSpecTestRunnerLookup.CurrentDescribedObject;

            if (lookupObj == null)
            {
                throw new CSpecException(ExceptionMessages.NoInterface);
            }

            ProxyTrace trace = (ProxyTrace)lookupObj.GetType().GetField("trace").GetValue(lookupObj);

            return(trace);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Runs the operations for a type, that inherit one of the Facade classes.
        /// </summary>
        /// <param name="testItem">CspecTestItem that describes the status of the running test</param>
        /// <param name="fields">class fields</param>
        /// <param name="description">the @it operation</param>
        /// <param name="obj">object that represents the spec</param>
        /// <param name="describedObject">the object that's beeing described</param>
        private void RunOperation(CSpecTestItem testItem, FieldInfo[] fields, Action<string> description, object obj, object describedObject)
        {
            MethodInfo beforeOp = null;
            MethodInfo afterOp = null;
            ProxyTrace trace = null;
            Type objType = obj.GetType();

            //Now call the operation methods.
            foreach (var field in fields.Where(x => x.FieldType.Name == "DescribeAll"
                || x.FieldType.Name == "Describe"
                ))
            {
                Delegate testRunner = (Delegate)field.GetValue(obj);
                testItem.Name = field.Name;

                if (BeforeOperation != null)
                    BeforeOperation(testItem);

                beforeOp = objType.GetMethod("BeforeOperation", BindingFlags.NonPublic | BindingFlags.Instance);
                afterOp = objType.GetMethod("AfterOperation", BindingFlags.NonPublic | BindingFlags.Instance);

                if (beforeOp != null)
                    beforeOp.Invoke(obj, null);

                try
                {
                    if (describedObject != null && describedObject.GetType().GetInterfaces().Length != 0)
                    {
                        trace = (ProxyTrace)describedObject.GetType().GetField("trace").GetValue(describedObject);
                        trace = new ProxyTrace();
                        describedObject.GetType().GetField("trace").SetValue(describedObject, trace);

                        //put to lookup
                        //NOTE: when doing multithreaded runners the entire section of this code
                        //should be locked by monitor
                        //NOTE: Moved to CSpecFacade constructor, it has much more sence there
                        //CurrentDescribedObject = describedObject;
                    }

                    if (field.FieldType.Name == "DescribeAll")
                    {
                        if (describedObject != null)
                            testRunner.DynamicInvoke(description, describedObject);
                        else
                            testRunner.DynamicInvoke(description);
                    }
                    else
                    {
                        ItAttribute it = (ItAttribute)field.GetCustomAttributes(typeof(ItAttribute), false)[0];
                        Console.WriteLine(it.Description);
                        testRunner.DynamicInvoke(null);
                    }

                    testItem.Results = "Test Passed!";
                    testItem.TestSucceed = true;

                    if (AfterOperation != null)
                        AfterOperation(testItem);

                    Passed++;
                }
                catch (System.Exception ex)
                {
                    HandleRunnerException(ex, testItem, trace);
                }

                if (afterOp != null)
                    afterOp.Invoke(obj, null);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handles the exceptions thrown by the testing method.
        /// </summary>
        private void HandleRunnerException(Exception ex, CSpecTestItem testItem, ProxyTrace trace)
        {
            Failed++;

            if (ex.InnerException != null)
                testItem.Results = "Test Failed: " + ex.InnerException.Message;
            else
                testItem.Results = "CSpec Exception: " + ex.Message;

            if (trace != null)
            {
                StringBuilder traceErrorBuilder = new StringBuilder();
                traceErrorBuilder.AppendLine("\n Trace: ");

                //foreach (var item in trace.MethodCalls)
                //    traceErrorBuilder.AppendLine(item);

                testItem.Results += traceErrorBuilder.ToString();
            }

            testItem.TestSucceed = false;

            if(AfterOperation != null)
                AfterOperation(testItem);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Runs the operations for a type, that inherit one of the Facade classes.
        /// </summary>
        /// <param name="testItem">CspecTestItem that describes the status of the running test</param>
        /// <param name="fields">class fields</param>
        /// <param name="description">the @it operation</param>
        /// <param name="obj">object that represents the spec</param>
        /// <param name="describedObject">the object that's beeing described</param>
        private void RunOperation(CSpecTestItem testItem, FieldInfo[] fields, Action <string> description, object obj, object describedObject)
        {
            MethodInfo beforeOp = null;
            MethodInfo afterOp  = null;
            ProxyTrace trace    = null;
            Type       objType  = obj.GetType();

            //Now call the operation methods.
            foreach (var field in fields.Where(x => x.FieldType.Name == "DescribeAll" ||
                                               x.FieldType.Name == "Describe"
                                               ))
            {
                Delegate testRunner = (Delegate)field.GetValue(obj);
                testItem.Name = field.Name;

                if (BeforeOperation != null)
                {
                    BeforeOperation(testItem);
                }

                beforeOp = objType.GetMethod("BeforeOperation", BindingFlags.NonPublic | BindingFlags.Instance);
                afterOp  = objType.GetMethod("AfterOperation", BindingFlags.NonPublic | BindingFlags.Instance);

                if (beforeOp != null)
                {
                    beforeOp.Invoke(obj, null);
                }

                try
                {
                    if (describedObject != null && describedObject.GetType().GetInterfaces().Length != 0)
                    {
                        trace = (ProxyTrace)describedObject.GetType().GetField("trace").GetValue(describedObject);
                        trace = new ProxyTrace();
                        describedObject.GetType().GetField("trace").SetValue(describedObject, trace);

                        //put to lookup
                        //NOTE: when doing multithreaded runners the entire section of this code
                        //should be locked by monitor
                        //NOTE: Moved to CSpecFacade constructor, it has much more sence there
                        //CurrentDescribedObject = describedObject;
                    }

                    if (field.FieldType.Name == "DescribeAll")
                    {
                        if (describedObject != null)
                        {
                            testRunner.DynamicInvoke(description, describedObject);
                        }
                        else
                        {
                            testRunner.DynamicInvoke(description);
                        }
                    }
                    else
                    {
                        ItAttribute it = (ItAttribute)field.GetCustomAttributes(typeof(ItAttribute), false)[0];
                        Console.WriteLine(it.Description);
                        testRunner.DynamicInvoke(null);
                    }

                    testItem.Results     = "Test Passed!";
                    testItem.TestSucceed = true;

                    if (AfterOperation != null)
                    {
                        AfterOperation(testItem);
                    }

                    Passed++;
                }
                catch (System.Exception ex)
                {
                    HandleRunnerException(ex, testItem, trace);
                }

                if (afterOp != null)
                {
                    afterOp.Invoke(obj, null);
                }
            }
        }