C# – Is it possible to Assert a method has been called in VS2005 Unit Testing

cunit testingvisual-studio-2005

I'm writing some unit tests and I need to be able to Assert whether a method has been called based upon the setup data.

E.g.

 String testValue = "1234";
 MyClass target = new MyClass();
 target.Value = testValue;

 target.RunConversion();

 // required Assertion
 Assert.MethodCalled(MyClass.RunSpecificConversion);

Then there would be a second test where testValue is null and I would want to assert that the method has NOT been called.


Update for specific test scenario:

I haev a class which represents information deserialized from XML. This class contains a number of pieces of information that I need to convert into my own classes.

The XML class is information about a person, including account info and a few phone numbers in different fields.

I have a method to create my Account class from the XML class, and methods to create the correct phone classes from the XML class. I have unit tests for each of these methods, but I'd like to test that when the account convertion is called, it runs the phone conversions as the results are actually properties of the account class.

I know I could test the properties of the account class after feeding in the correct information, however I have otehr nested properties that have further nested and testing the entire tree could become very cumbersome. I guess I could just have each level test the next level below it, but ideally I'd like to make sure the correct conversino methods are being called and the code is not being duplicated in the implementation.

Best Answer

Without using a Mocking framework such as Moq, TypeMock, RhinoMocks that can verify your expectations, I would look at parsing the stack trace.

The MSDN documentation here should help.

Kindness,

Dan