I've decided to create Unit Tests for a WinForm application I have. I used a custom layered MVC approach (View, Controller, Model) which has made unit testing easier by creating a mock view and testing the controller methods. I've encountered an interesting problem with exceptions. In my application, exceptions are propagated via events. My controller subscribes to an "Exception Event" contained in the model which has exception information. In the event handler, the controller takes that information and calls the View's "Display Error" method. Here's some partial code to depict what I'm doing:
interface IView
{
public void DisplayError(string message);
}
public class Controller
{
IView _view;
Model _model;
public Controller(IView view, Model model)
{
_view = view;
_model = model;
model.ErrorRaised += ErrorRaisedEventHandler(handle_error);
}
private void handle_error(object sender, ErrorEventArgs e)
{
_view.DisplayError(e.Message);
}
}
public Model
{
event ErrorRaisedEventHandler ErrorRaised;
public void DoSomething()
{
try
{
//Do something bad
}
catch (Exception e)
{
ErrorRaised(this, new ErrorEventArgs(e.Message))
}
}
}
Is there a best practice for unit testing this? I was searching for something along the lines of asserting the output message from the exceptions but didn't get very far. Thanks!
Aucun commentaire:
Enregistrer un commentaire