コード例 #1
0
        public static TException Throws <TException>(Action action, params string[] possibleMessages)
            where TException : Exception
        {
            try
            {
                action();

                Assert.Fail("Exception of type {0} expected. No exception thrown.", typeof(TException).Name);
                return(null);
            }
            catch (TException ex)
            {
                if (possibleMessages == null || possibleMessages.Length == 0)
                {
                    return(ex);
                }
                foreach (string possibleMessage in possibleMessages)
                {
                    if (StringAssert.Equals(possibleMessage, ex.Message))
                    {
                        return(ex);
                    }
                }

                throw new Exception("Unexpected exception message." + Environment.NewLine + "Expected one of: " + string.Join(Environment.NewLine, possibleMessages) + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex);
            }
        }
コード例 #2
0
 /// <summary>
 /// Don't run action but let a compiler detect the code in action as an executable block.
 /// </summary>
 public static void Ensure(Newtonsoft.Json.Serialization.Action action)
 {
     if (IsFalse())
     {
         try
         {
             action();
         }
         catch (Exception e)
         {
             throw new InvalidOperationException("", e);
         }
     }
 }