Compartilhar via


Bookmark Collection: Checking Input Parameters

The tests that I am going to implement this time around have to do with the input parameters to the Add method. Both input parameters in this example cannot be null. If they are null then the Add method should throw ArgumentNullException. Here's the test:

[

Test, ExpectedException(typeof(ArgumentNullException))]
public void AddNullLabelBookmark()
{
collection.Add(null, exampleDotComUri);
}

When I run this test it passes, seems like I lucked out. It just so happens that the underlying collection that I am using in the BookmarkCollection throws ArgumentNullException when the key value is null. I don't want to depend on this behavior so I am going to modify the Add method as follows:

public void Add(string label, Uri site)
{
if (label == null)
throw new ArgumentNullException();

dictionary[label] = site;
}

That's better. It's not the simplest thing that could work but relying on an underlying collection for the explicit behavior of the BookmarkCollection does not seem like the right thing to do. The code compiles and the test passes. Let's switch gears and look at the Uri parameter:

[

Test, ExpectedException(typeof(ArgumentNullException))]
public void AddNullUriBookmark()
{
collection.Add(exampleDotComLabel, null);
}

When I run this test it fails. Let's change the implementation:

public void Add(string label, Uri site)
{
if (label == null || site == null)
throw new ArgumentNullException();

dictionary[label] = site;
}

After I make this change the code compiles and all of the tests pass.