Hi,
I have the following code, which trys to test an exceptioin being thrown and I came across some strange behaviour.
namespace boo
{
internal Class foo
{
private void ParseConsistsTag (string line)
{
string[] parts = line.Replace (" ", String.Empty).Split (':');
if (parts.Length != 3)
throw new MaterialInvalidArgumentException (String.Format ("The line <0> was illegal. It should have had the segments 'ID:Name:Percentage'.", line));
// ....
}
}
namespace Exceptions
{
[Serializable]
public class MaterialInvalidArgumentException : Exception
{
public MaterialInvalidArgumentException () { }
public MaterialInvalidArgumentException (string message) : base (message) { }
public MaterialInvalidArgumentException (string message, Exception inner) : base (message, inner) { }
protected MaterialInvalidArgumentException (SerializationInfo info, StreamingContext context) : base (info, context) { }
}
}
namespace Test.boo
{
[Test, Isolated]
[ExpectedException(typeof(MaterialInvalidArgumentException))]
public void Test.foo
{
string[] lines = new[] { "Additiv05 : 10", " : Input09 : 85", ": Additiv13 : 5" };
foo = new foo();
Isolate.NonPublic.WhenCalled(txt,"ParseConsistsTag").WillThrow (new MaterialInvalidArgumentException());
foreach (var line in lines)
Isolate.Invoke.Methode (foo, "ParseConsistsTag", line);
// ...
}
}
Ultimately this results in 3 thrown exceptions but the messages got lost somehow or at least it is not displayed. But more important the test fails with 3 exceptions being thrown. Which is wrong since the parameters handed in are correct on call 2 & 3. Since I am testing for exceptions being thrown the test should fail but tell me that it has one exception catched and twice the code does not meet the expectation.
If however I do the following everything works fine and I have the messages and the test completes successfully.
namespace Test.boo
{
[Test, Isolated]
public void ParseConsistsTag_InCorrectConsistsMaterialLine_ThrowsMaterialInvalidArgumentException ()
{
string[] lines = new[] { "Additiv05 : 10", " : Input09 : 85", ": Additiv13 : 5" };
foo txt = new foo ();
foreach (string line in lines)
try
{
Isolate.Invoke.Method (foo, "ParseConsistsTag", line);
}
catch (MaterialInvalidArgumentException ex)
{
// Info: Should be a call to the logger!
Console.WriteLine ("ERROR: {0}", ex.Message);
}
// ...
}
}
}
What am I doing wrong in the first place or do I misunderstand here something?
Thank's for any hint or help!
Peter