I wrote the following code:
[TestMethod]
//[ExpectedException(typeof(UriFormatException), "url should be well formatted.")]
public void FetchHtmlContent_badUrl_throwUriFormatException()
{
HashSet<string> urls = new HashSet<string> { "ww.stackoverflow.com" };
var contextManager = new ContentManager(urls);
var content = contextManager.GetHtmlContent();
Assert.IsTrue(content.ElementAt(0).Contains("threw an exception of type 'System.UriFormatException'"));
}
contextManager.GetHtmlContent() doesn’t throw an exception,
but content.ElementAt(0) throws (as expected)
+ content.ElementAt(0) 'content.ElementAt(0)' threw an exception of type 'System.UriFormatException' string {System.UriFormatException}
How can I verify content.ElementAt(0) throws this exception
(or should I verify this test some other way ?)
What’s the responsibility of the
ContentManager.GetHtmlContentmethod? If as name would indicate, retrieving HTML content from URL, then invalid url is execution failure scenario (method cannot do what it is supposed to). You have two choices:.GetHtmlContentmethod (communicates well what happens, and follows Microsoft guidelines)nullfrom.GetHtmlContentand deal with that laterNote that returning null result might also be used for a case when HTML content is indeed
null, therefore I suggest throwing exception here. It states what happened in more clear way.Your test might look like that: