I'm a little confused at what's going on here. I'm looking at the Puzzle example from Atomic Object showing how to test a Model-View-Presenter pattern Puzzle.zip
The View has a private event. The view also has a Subscribe(delegate) function that adds the delegate to the event. The Presenter is passed in an IView and an IModel. During construction, it subscribes to the view and hooks it up to a function on the model.
For unit testing the Presenter, the View class is mocked using NMock. So it is just a dumb class, and the Subscribe() function doesn't actually do anything. Of course, to test the presenter, you have to mock up the view and model, then trigger the event in the view and ensure the model function was called. The example code works just fine - however, I don't understand how it works!!
Some excerpts:
private DynamicMock modelMock;
private IPuzzleModel model;
private DynamicMock viewMock;
private IPuzzleView view;
private SavedTypeOf moveRequestConstraint;
[SetUp]
public void SetUp()
{
modelMock = new DynamicMock(typeof(IPuzzleModel));
modelMock.Strict = true;
model = modelMock.MockInstance as IPuzzleModel;
// Setup the view
viewMock = new DynamicMock(typeof(IPuzzleView));
viewMock.Strict = true;
view = viewMock.MockInstance as IPuzzleView;
moveRequestConstraint = new SavedTypeOf(typeof(PointDelegate));
viewMock.Expect("SubscribeMoveRequest", moveRequestConstraint);
// create the presenter
new PuzzlePresenter(model, view);
}
[Test]
public void test_MoveRequest_fromView()
{
Point point = new Point(1, 2);
modelMock.Expect("MoveRequest", point);
PointDelegate trigger = moveRequestConstraint.GetInstance as PointDelegate;
trigger(point);
}
Somehow, the "trigger(point)" call is actually connected to the view, and is causing the private event in the view to trigger. I can't figure out how this is working - I don't see where it is connected to the view instance. What am I missing?
Update: I am trying to use NMock 2. It appears that the moveRequestConstraint variable receives the value that is passed into SubscribeMoveRequest() in the TestSetup function. However, that is NMock 1 syntax - and NMock 2 does not appear to support that syntax. How would I do it with NMock 2?