NUnit is a testing tool that allows you to write unit tests
for your .NET applications. Although not a cornerstone to creating a custom
provider, they are an integral part of validating the correctness of the code, and
are a key feature of iterative development methodologies. In addition to the
unit tests, I use a SQL Express database that contains an exact replica of the
tables/stored procedures, along with sample data that I can use to conduct more
meaningful tests. The great benefit with SQL Express is that I can declare it
to "Copy Always" as a build action, and it overwrites the modified
data with the original, ensuring my tests can run every time.
The problem with the database is that it retains the state
of the data, so any modifications in code must be managed carefully. That is
why SQL Express works out very nicely, as it works by changing the connection
string to the local version copied to the bin folder. The NUnit tests rely on
an unaltered copy of the database, which is why tests can only be run once at a
time after building. Take a look at the tests; below is some of my various
unit testing code:
Listing 13
[Test]
public void TestAddNewsletter()
{
Newsletter.AddNewsletter("My e-Business Weekly", "This is about e-Business");
}
[Test]
public void TestAddSubscription()
{
Newsletter.AddSubscription("df@tvchannel.tv", "Stock Exchange Weekly");
}
[Test]
public void TestFindSubscribers()
{
List<string> newslettersList =
new List<string>(Newsletter.FindSubscriber("yahoo.com", "SE Weekly"));
Assert.IsNotNull(newslettersList);
Assert.AreEqual(2, newslettersList.Count);
Assert.IsTrue(newslettersList.Contains("bgmst5@yahoo.com"));
Assert.IsTrue(newslettersList.Contains("dsfsadf@yahoo.com"));
}
[Test]
public void TestGetAllNewsletters()
{
//Use list for easier access to data
List<string> newslettersList =
new List<string>(Newsletter.GetAllNewsletters());
Assert.IsNotNull(newslettersList);
Assert.AreEqual(4, newslettersList.Count);
Assert.IsTrue(newslettersList.Contains("Business Weekly"));
Assert.IsTrue(newslettersList.Contains("SE Weekly"));
Assert.IsTrue(newslettersList.Contains("Forbes 500 Newsletter"));
Assert.IsTrue(newslettersList.Contains("My e-Business Weekly"));
}
[Test]
public void TestSubscriberExists()
{
Assert.IsTrue(Newsletter.SubscriberExists("bmains@hotmail.com"));
Assert.IsFalse(Newsletter.SubscriberExists("failed@yahoo.com"));
}
These various kinds of tests ensure that the results
returned are the exact values wanted, and that the insertions that are
performed are working and in proper function. With all the code, you want to
ensure that successful cases work correctly. In addition, because the table
has an application name parameter, the tests ensure that no data comes from
outside application names. Successful cases aren't also tested; in addition,
the tests verify that the correct error responses occur when we test with
invalid data. For instance, the following tests verify that exceptions are
thrown upon duplicate and nullable data:
Listing 14
[Test, ExpectedException(typeof(ArgumentException))]
public void TestAddSubscriptionDuplication()
{
Newsletter.AddSubscription("asdsadfsdfdsf@gmail.com", "Business Weekly");
}
[Test, ExpectedException(typeof(ArgumentNullException))]
public void TestAddSubscriptionNullable()
{
Newsletter.AddSubscription(null, "Business Weekly");
}
In the above situation, the ExpectedException handles the
error checking, which requires that an exception is thrown or the statement is
failed.