示例#1
0
        public sealed override void OnEntry(MethodExecutionArgs args)
        {
            // Look for override type (by convention)
            string overrideTypeName = GetExpectedOverrideTypeName(args.Method.DeclaringType, "Overrides");

            Type overrideType;

            // If no override type found, let method invocation proceed
            if (!TryGetOverrideType(overrideTypeName, out overrideType))
                return;

            MethodInfo overrideMethod;
            
            // If there's no corresponding method on override type, quit now
            if (!TryGetOverrideMethod(overrideType, args.Method, out overrideMethod)) 
                return;

            // Construct the override class
            var overideStepsInstance = ConstructOverrideInstance(overrideType, overrideTypeName);

            // Invoke the override method
            overrideMethod.Invoke(overideStepsInstance, args.Arguments.ToArray());

            // Don't allow call through to "Core" steps method
            args.FlowBehavior = FlowBehavior.Return;
        }
        public sealed override void OnEntry(MethodExecutionArgs args)
        {
            // If this is not an NUnit Test, let method invocation proceed
            if (!IsUnitTestMethod(args.Method)) 
                return;

            // Look for an override Feature in "Web" (by convention)
            string overrideTypeName = GetExpectedOverrideTypeName(args.Method.DeclaringType, string.Empty);

            Type overrideType;

            // If no override type found, let method invocation proceed
            if (!TryGetOverrideType(overrideTypeName, out overrideType))
                return;

            MethodInfo overrideMethod;

            // If there's no corresponding method on the override type, let method invocation proceed
            if (!TryGetOverrideMethod(overrideType, args.Method, out overrideMethod))
                return;

            // If this is not an NUnit Test, let method invocation proceed
            if (!IsUnitTestMethod(overrideMethod))
                return;

            // If we're still here, we have an override of the feature test in the "Web" project

            // Make SpecFlow happy, contextually speaking (we're about to skip the test, and this 
            // causes bad things to happen if certain calls aren't made)
            MakeSpecFlowHappy(args, overrideTypeName);

            // Don't allow "Core" unit test to run
            args.FlowBehavior = FlowBehavior.Return;
        }
        private static void MakeSpecFlowHappy(MethodExecutionArgs args, string overrideTypeName)
        {
            var declaringType = args.Method.DeclaringType;
            var testRunnerField = declaringType.GetField("testRunner", BindingFlags.Static | BindingFlags.NonPublic);
            ScenarioInfo scenarioInfo = new ScenarioInfo("[Skipping due to override]", null);
            var testRunner = testRunnerField.GetValue(null) as ITestRunner;
            testRunner.OnScenarioStart(scenarioInfo);

            Assert.Inconclusive("Skipped due to override method '{0}' located in '{1}'.", 
                args.Method.Name, overrideTypeName);
        }
        public sealed override void OnException(MethodExecutionArgs args)
        {
            IEnumerable<string> arguments = args.Arguments.Select(o => o == null ? "[null]" : o.ToString());

            string context = string.Format("{0}({1})",
                                           args.Method.Name,
                                           string.Join(",", arguments));

            ScenarioContext.Current.GetBrowser().SaveScreenshot(context);

            base.OnException(args);
        }