views:

74

answers:

5

Does anybody know of a way to create a DataSet such that when I call GetXml() on the DataSet I will get an exception? I'm trying to write some failure testing for this scenario and am having a hard time. Is it possible?

A: 

You could simply throw the exception that a corrupted dataset would throw to emulate the problem

Bryan Denny
No, the method I'm testing calls dataSet.GetXML() and I need that to fail so it will hit the catch block (trying to get full code coverage).
dcp
+1  A: 
jball
You can refer to this link: http://bytes.com/topic/net/answers/827334-dataset-getxml-returns-child-row-has-multiple-parents-exceptionIt seems it is possible to get an exception.
dcp
Interesting - I'll update my answer with a technique that should give you that exception.
jball
A: 

According to the documentation the DataSet.GetXML method does not throw exceptions.

It may return an empty string or null (although the docs don't say so) but it doesn't throw exceptions.

You might avoid exceptions by first checking if the resulting DataSet contain any errors (DataSet.HasErrors) or even go so far as to check every table (DataTable.HasErrors) and every row (DataRow.HasErrors)?

Are you sure about that? http://bytes.com/topic/net/answers/827334-dataset-getxml-returns-child-row-has-multiple-parents-exception
dcp
A: 

Could you pass a null DataSet object to those functions which are calling GetXML? :-)

ydobonmai
Yes, but that gets ArgumentNullException thrown, which is in a different catch. Thanks anyway.
dcp
Alright, what kind of exception are you looking to catch?
ydobonmai
Anything besides ArgumentNullException
dcp
Have you already tried :-http://bytes.com/topic/net/answers/827334-dataset-getxml-returns-child-row-has-multiple-parents-exception
ydobonmai
+2  A: 

Can you write a subclass of DataSet such that its getXml() predictably throws an Exception?

Jim Kiley