public static T Obtain <T>(this SpecFlowContext context, string key = null)
            where T : new()
        {
            T instance;

            return(context.TryGetValue(key, out instance) ? instance : new T());
        }
Beispiel #2
0
 public static void AssertNoLastException(SpecFlowContext context)
 {
     if (context.TryGetValue(LastExceptionContextKey, out Exception ex))
     {
         Assert.Fail($"Expected no last exception, but an exception of type '{ex.GetType().Name}' was thrown: '{ex.Message}'");
     }
 }
        private static void RethrowTeardownExceptions(SpecFlowContext context, string key)
        {
            context.Set(false, TeardownBindingPhaseKey);

            if (context.TryGetValue(key, out List <Exception> elist))
            {
                throw new AggregateException(elist);
            }
        }
        /// <summary>Gets a value from the context using a specified key.  Asserts if the key is not found.</summary>
        /// <typeparam name="T">The type of the value to return.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="key">The key of the value to retrieve.</param>
        /// <returns>The value associated with the specified key.</returns>
        public static T GetEx <T>(this SpecFlowContext context, string key)
        {
            if (!context.TryGetValue(key, out T value))
            {
                throw new Exception($"'{key}' was not found.");
            }

            return(value);
        }
Beispiel #5
0
        public static void AssertLastExceptionWasOfType(SpecFlowContext context, string expectedExceptionTypeName)
        {
            if (!context.TryGetValue(LastExceptionContextKey, out Exception exception))
            {
                Assert.Fail($"Expected an exception of type '{expectedExceptionTypeName}' to have been thrown, but none was.");
            }

            Assert.AreEqual(expectedExceptionTypeName, exception.GetType().Name);
        }
Beispiel #6
0
        private static bool VerifyBindingAvailable(SpecFlowContext featureContext)
        {
            if (!featureContext.TryGetValue(ContainerBindingPhaseKey, out bool serviceBuildInProgress))
            {
                throw new InvalidOperationException(
                          $"This method requires {typeof(ContainerBindings).FullName} to be registered with SpecFlow and either the @perScenarioContainer or the @perFeatureContainer tag to be specified");
            }

            return(serviceBuildInProgress);
        }
Beispiel #7
0
        private static void StoreExceptionInContext(SpecFlowContext context, Exception x, string key)
        {
            if (!context.TryGetValue(key, out List <Exception> elist))
            {
                elist = new List <Exception>();
                context.Add(key, elist);
            }

            elist.Add(x);
        }
        /// <summary>
        /// Gets the current instance of the manager from the given context.
        /// </summary>
        /// <param name="context">The <see cref="SpecFlowContext"/> for the manager.</param>
        /// <returns>
        /// The instance of the <see cref="OpenApiWebHostManager"/> for the current feature/scenario.
        /// </returns>
        public static OpenApiWebHostManager GetInstance(SpecFlowContext context)
        {
            if (!context.TryGetValue(ContextKey, out OpenApiWebHostManager manager))
            {
                manager = new OpenApiWebHostManager();
                context.Set(manager, ContextKey);
            }

            return(manager);
        }
Beispiel #9
0
        public static T LazyLoad <T>(this SpecFlowContext specFlowContext, Func <T> creation) where T : class
        {
            T item;

            if (!specFlowContext.TryGetValue(out item))
            {
                item = creation();
                specFlowContext.Set(item);
            }

            return(item);
        }
Beispiel #10
0
        public static Exception GetLastException(SpecFlowContext context)
        {
            if (context.TryGetValue(LastExceptionContextKey, out Exception ex))
            {
                return(ex);
            }

            Assert.Fail("Expected an exception to have been thrown, but was not.");

            // This looks odd, but is only here to calm down the compiler. In practice, this code is unreachable.
            return(null !);
        }
        ///// <summary>
        ///// Gets a value from the context or parent context(s) using a specified key.  Adds and returns a specified
        ///// default value if the key was not found.
        ///// </summary>
        ///// <typeparam name="T">The type of the value to return.</typeparam>
        ///// <param name="context">The context.</param>
        ///// <param name="defaultValue">The value to return if the value was not found.</param>
        ///// <returns>The value associated with the default key or the default value if not found.</returns>
        //public static T GetInheritedOrDefault<T>(this SpecFlowContext context, T defaultValue)
        //    => GetInheritedOrDefault(context, typeof(T).FullName, defaultValue);

        /// <summary>
        /// Gets a value from the context using a specified key.  Adds and returns a specified default value if the
        /// key was not found.
        /// </summary>
        /// <typeparam name="T">The type of the value to return.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The value to return if the key was not found.</param>
        /// <returns>The value associated with the specified key or the default value if not found.</returns>
        public static T GetOrDefault <T>(this SpecFlowContext context, string key, T defaultValue)
        {
            T value;

            if (context.TryGetValue(key, out value))
            {
                return(value);
            }

            value = defaultValue;
            context.Set(value, key);
            return(value);
        }
        /// <summary>
        /// Called by helpers that only work if this binding is in place.
        /// </summary>
        /// <param name="context">The SpecFlow context.</param>
        internal static void VerifyBindingAvailable(SpecFlowContext context)
        {
            if (!context.TryGetValue(TeardownBindingPhaseKey, out bool exceptionsNotYetRethrown))
            {
                throw new InvalidOperationException(
                          $"This method requires {typeof(TeardownExceptionsBindings).FullName} to be registered with SpecFlow");
            }

            if (!exceptionsNotYetRethrown)
            {
                throw new InvalidOperationException(
                          "This method must be called during BeforeFeature bindings with an Order < int.MaxValue");
            }
        }
        /// <summary>
        /// Gets a value from the context or parent context(s) using a specified key.  Asserts if the key is not
        /// found.
        /// </summary>
        /// <typeparam name="T">The type of the value to return.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="key">The key of the value to retrieve.</param>
        /// <returns>The value associated with the default key.</returns>
        public static T GetInherited <T>(this SpecFlowContext context, string key)
        {
            if (context.TryGetValue(key, out T value))
            {
                return(value);
            }

            //if (context is ScenarioStepContext)
            //{
            //    if (context.GetScenarioContext()?.TryGetValue(key, out value) ?? false) return value;
            //    if (context.GetFeatureContext()?.TryGetValue(key, out value) ?? false) return value;
            //}

            if (context is ScenarioContext)
            {
                if (context.GetFeatureContext()?.TryGetValue(key, out value) ?? false)
                {
                    return(value);
                }
            }

            throw new Exception($"'{key}' was not found.");
        }