Working with Custom Providers
page 6 of 8
by Brian Mains
Feedback
Average Rating: 
Views (Total / Last 10 Days): 53798/ 95

Part 5: NUnit Tests

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.


View Entire Article

User Comments

Title: Source Code   
Name: Satish Nandigam
Date: 2010-10-27 2:43:31 AM
Comment:
Hi This is a nice article . Can please provide the source code for custom Provider for the Newsletter.
thanks,
N.Satish
Title: Thanks   
Name: Anitha T S
Date: 2010-07-26 7:31:51 AM
Comment:
Thank you very much for your article on providers. Your article is easy to read and understand! I am a better because of it. ;)
Title: Question Reply   
Name: Brian Mains
Date: 2009-10-16 2:53:52 PM
Comment:
Hello,

Yes, DefaultProvider doesn't exist within ConfigurationSection; it exists in my custom base class, which I should have posted, but I didn't. My apologies.

In your custom section class, just add:

[ConfigurationProperty("defaultProvider")]
public string DefaultProvider
{
get { return (string)this["defaultProvider"]; }
}

[
ConfigurationProperty("providers", IsDefaultCollection=false),
ConfigurationCollection(ProviderSettingsCollection)
]
public ProviderSettingsCollection Providers
{
get { return (ProviderSettingsCollection)this["providers"]; }
}

That's what exists in my base class, as a helper. You can also download the project at: http://www.codeplex.com/nucleo, which has these files in Nucleo.dll, in the Nucleo.Providers namespace. I will be posting an update to this project soon with updated AJAX controls, but this code hasn't been touched so it will remain the same, if you are interested.
Title: Question   
Name: Mark Toth
Date: 2009-10-16 2:08:41 PM
Comment:
When I derive a class from ConfigurationSection I get the following error for DefaultProvider "no suitable method found to override". Am I missing something?
Title: Thanks   
Name: Mahr G. Mohyuddin
Date: 2009-04-22 9:21:56 AM
Comment:
Well explained, Brian!. Thanks.
Title: Many Thanks   
Name: Linda
Date: 2009-02-11 9:58:01 AM
Comment:
Thank you very much for your article on providers. Your article is easy to read and understand! I am a better because of it. ;)
Title: still confused reply   
Name: Brian
Date: 2008-08-28 8:50:50 AM
Comment:
The static class is a class separate from the rest of the code, which exposes the provider base class to the public. It's responsible for instantiating it.

SO this is something that should be in the same project as the provider, but is a separate class.
Title: still confused :(   
Name: .
Date: 2008-08-28 3:19:45 AM
Comment:
Would have been nice to be able to download code. I'm at a loss as to where to put the static class - whether I put it in the application which is trying to use the providers, or in the provider code itself as a separate class.
Title: good articles   
Name: I LIKE LT
Date: 2007-09-05 9:16:47 PM
Comment:
very good articles
Title: Good   
Name: Bilal Wani
Date: 2007-03-20 7:17:27 AM
Comment:
Nice Article!!!
Title: Patil   
Name: Sandip
Date: 2007-03-15 5:16:26 PM
Comment:
Nice Article!!!

-Sandip Patil
Title: Good   
Name: Ramamuni Reddy
Date: 2007-02-18 11:25:54 PM
Comment:
Hello Brian Mains,
Very Good Article.

With Regrads
Ramamuni reddy Mulapaku
Title: Provider Utility   
Name: Bilal Hadiar [MVP]
Date: 2007-02-06 6:07:46 AM
Comment:
Hello Brian,
It is a well written article, congratulations!

I would like to refer you and all the readers to a utility I created a while ago that helps you generate the skeleton of a provider files in a single button click,
Check it here:
http://bhaidar.net/cs/archive/2006/07/07/376.aspx

Regards
Title: Mr.   
Name: KotiReddy.
Date: 2007-02-06 12:30:15 AM
Comment:
Very Good Article.


Regards,
Koti Reddy. S






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-05 12:17:27 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search