Lets say I have a eventhandler like so:
public SomethingHappended_Handler(object sender, EventArgs e)
{
var myobj = sender as MyClass();
myobj.DoSomethingImportant();
}
How can I test that myObj is the type of MyClass? I know that the sender will always be of MyClass but I just thought to be safe I would need a:
if (myObj != null) //Close it
If the type is not MyClass then myObj will be null. As I know its best to test for the not null how can I create a unit test to prove that if I send a different type as the sender other than MyClass it will fall over calling DoSomethingImportant and therefore I need the null check?
I can answer you in pseudo-code to point out what you could do.
First off you can check if sender is of type MyClass.
Then in your unittest you can send in two mock objects, one of type MyClass and one of another type “MyClassFake”. Make an interface both use and make them implement the Close() method.
Make two unittests:
Close() is just an example, if you have other methods, you can test called/not called against them.