예제 #1
0
        public void ReferenceEqualsFailsWhenUsed()
        {
            var ex = Assert.Throws <InvalidOperationException>(() => FileAssert.ReferenceEquals(string.Empty, string.Empty));

            Assert.That(ex.Message, Does.StartWith("FileAssert.ReferenceEquals should not be used for Assertions"));
        }
예제 #2
0
        public void TestAssertions()
        {
            #region Condition assertions

            Assert.True(true, "Assert.True and Assert.IsTrue test that the specified condition is true. ");


            Assert.IsTrue(true);

            Assert.False(false, "Assert.False and Assert.IsFalse test that the specified condition is false.");
            Assert.IsFalse(false);

            Assert.Null(null);
            Assert.IsNull(null, "Assert.Null and Assert.IsNull test that the specified object is null.");
            Assert.IsNotNull("10");
            Assert.NotNull("10", "");

            Assert.IsNaN(Double.NaN, "Assert.IsNaN tests that the specified double value is NaN (Not a number).");

            Assert.IsEmpty("");
            Assert.IsEmpty(new List <object>());
            Assert.IsNotEmpty(new List <object> {
                1
            });
            Assert.IsNotEmpty("MyTestString");

            #endregion

            #region Equality

            Assert.AreEqual(true, true, "Assert.AreEqual tests whether the two arguments are equal.");
            Assert.AreNotEqual(true, false);

            #endregion

            #region Identity

            Assert.AreSame(string.Empty, string.Empty,
                           "Assert.AreNotSame tests that the two arguments do not reference the same object.");
            Assert.AreNotSame(new object(), new object());

            //both objects are refering to same object
            object a = new object();
            object b = a;
            Assert.AreSame(a, b);
            #endregion

            #region Comparision

            //Contrary to the normal order of Asserts, these methods are designed to be read in the "natural" English-language or mathematical order.
            //Thus Assert.Greater(x, y) asserts that x is greater than y (x > y).
            Assert.Greater(Decimal.MaxValue, Decimal.MinValue,
                           "Assert.Greater tests whether one object is greater than than another");
            Assert.GreaterOrEqual(Decimal.MinValue, Decimal.MinValue);

            Assert.Less(Decimal.MinValue, Decimal.MaxValue);
            Assert.LessOrEqual(Decimal.MinValue, Decimal.MinValue);

            #endregion

            #region Types

            Assert.IsInstanceOf <decimal>(decimal.MinValue,
                                          "Assert.IsInstanceOf succeeds if the object provided as an actual value is an instance of the expected type.");
            Assert.IsNotInstanceOf <int>(decimal.MinValue);

            Assert.IsNotAssignableFrom(typeof(List <Type>), string.Empty,
                                       "Assert.IsAssignableFrom succeeds if the object provided may be assigned a value of the expected type.");
            Assert.IsAssignableFrom(typeof(List <decimal>), new List <decimal>());

            Assert.IsNotAssignableFrom <List <Type> >(string.Empty);
            Assert.IsAssignableFrom <List <decimal> >(new List <decimal>());

            #endregion


            #region Strings

            //The StringAssert class provides a number of methods that are useful when examining string values
            StringAssert.Contains("london", "london");
            StringAssert.StartsWith("Food", "FoodPanda");
            StringAssert.EndsWith("rangers", "Powerrangers");
            StringAssert.AreEqualIgnoringCase("DOG", "dog");
            StringAssert.IsMatch("[10-29]", "15");
            StringAssert.DoesNotContain("abc", "defghijk");
            StringAssert.DoesNotEndWith("abc", "abcdefgh");
            StringAssert.DoesNotMatch("abc", "def");
            string a1 = "abc";
            string b1 = "abcd";
            //StringAssert.ReferenceEquals(a1, b1); need to debug why it's failing

            Assert.Contains(string.Empty, new List <object> {
                string.Empty
            },
                            "Assert.Contains is used to test whether an object is contained in a collection.");

            #endregion

            #region Collections

            //The CollectionAssert class provides a number of methods that are useful when examining collections and
            //their contents or for comparing two collections. These methods may be used with any object implementing IEnumerable.


            //The AreEqual overloads succeed if the corresponding elements of the two collections are equal.
            //AreEquivalent tests whether the collection contents are equal, but without regard to order.
            //In both cases, elements are compared using NUnit's default equality comparison.

            CollectionAssert.AllItemsAreInstancesOfType(new List <decimal> {
                0m
            }, typeof(decimal));
            CollectionAssert.AllItemsAreNotNull(new List <decimal> {
                0m
            });
            CollectionAssert.AllItemsAreUnique(new List <decimal> {
                0m, 1m
            });
            CollectionAssert.AreEqual(new List <decimal> {
                0m
            }, new List <decimal> {
                0m
            });
            CollectionAssert.AreEquivalent(new List <decimal> {
                0m, 1m
            },
                                           new List <decimal> {
                1m, 0m
            });                              // Same as AreEqual though order does not mater
            CollectionAssert.AreNotEqual(new List <decimal> {
                0m
            }, new List <decimal> {
                1m
            });
            CollectionAssert.AreNotEquivalent(new List <decimal> {
                0m, 1m
            },
                                              new List <decimal> {
                1m, 2m
            });                              // Same as AreNotEqual though order does not matter
            CollectionAssert.Contains(new List <decimal> {
                0m, 1m
            }, 1m);
            CollectionAssert.DoesNotContain(new List <decimal> {
                0m, 1m
            }, 2m);
            CollectionAssert.IsSubsetOf(new List <decimal> {
                1m
            },
                                        new List <decimal> {
                0m, 1m
            });                              // {1} is considered a SubSet of {1,2}
            CollectionAssert.IsNotSubsetOf(new List <decimal> {
                1m, 2m
            }, new List <decimal> {
                0m, 1m
            });
            CollectionAssert.IsEmpty(new List <decimal>());
            CollectionAssert.IsNotEmpty(new List <decimal> {
                1m
            });
            CollectionAssert.IsOrdered(new List <decimal> {
                1m, 2m, 3m
            });
            CollectionAssert.IsOrdered(new List <string> {
                "a", "A", "b"
            }, StringComparer.CurrentCultureIgnoreCase);
            CollectionAssert.IsOrdered(new List <int> {
                3, 2, 1
            },
                                       "The list is ordered"); // Only supports ICompare and not ICompare<T> as of version 2.6

            #endregion

            #region File Assert

            //Various ways to compare a stream or file.
            //The FileAssert class provides methods for comparing or verifying the existence of files
            //Files may be provided as Streams, as FileInfos or as strings giving the path to each file.
            FileAssert.AreEqual(new MemoryStream(), new MemoryStream());
            FileAssert.AreEqual(new FileInfo("MyFile.txt"), new FileInfo("MyFile.txt"));
            FileAssert.AreEqual("MyFile.txt", "MyFile.txt");
            FileAssert.AreNotEqual(new FileInfo("MyFile.txt"), new FileInfo("MyFile2.txt"));
            FileAssert.AreNotEqual("MyFile.txt", "MyFile2.txt");
            FileAssert.AreNotEqual(new FileStream("MyFile.txt", FileMode.Open),
                                   new FileStream("MyFile2.txt", FileMode.Open));

            FileAssert.Equals(new FileInfo("MyFile.txt"), new FileInfo("MyFile.txt"));
            FileAssert.ReferenceEquals(new FileInfo("MyFile.txt"), new FileInfo("MyFile.txt"));

            #endregion

            #region Utilities

            // Used to stop test execution and mark them pass or fail or skip

            if (Convert.ToInt32(DateTime.Now.Second) > 30)
            {
                Console.WriteLine("Exaple on Utilities assertions");
                Assert.Ignore();
            }

            if (Convert.ToInt32(DateTime.Now.Second) < 30)
            {
                Assert.Inconclusive();
            }

            // Defining the failed message
            Assert.IsTrue(true, "A failed message here");

            Assert.Pass();
            Assert.Fail();

            #endregion
        }