I'm doing some unit tests for a controller, and I'm mocking the business component. The BC has a public event that I have the controller listening to when the controller is constructed.
The problem I'm having is I keep getting an Expectation error stating: "IBC.add_MessageRaised(MessageEventHandler) Expected#:1 Actual#:0".
However, I don't have any expectation of that kind in my test. I'm wondering if it has to do with setting the Controller to listen to an event on a mocked object (the BC in this case). Is there another way I can get the Controller to listen to an event coming from a mock?
I'm also trying to think of a way to get the mock to raise the MessageRaised event, but that might be another question altogether.
Here is the code:
Business Component Interface
public interface IBC
{
event MessageEventHandler MessageRaised;
XmlDocument GetContentXml(string path);
}
Controller
private readonly IBC _bc;
public Controller(IBC bc)
{
_bc = bc;
_bc.MessageRaised += MessageWatch;
}
private void MessageWatch(object sender, MessageEventArgs e)
{
if (MessageRaised != null)
MessageRaised(sender, e);
}
Unit Test
MockRepository Mockery = new MockRepository();
TFactory _tFac;
IView _view;
Presenter _presenter = new Presenter();
IBC _bc = Mockery.DynamicMock<IBC>();
Controller _controller = new Controller(_bc);
_tFac = new TFactory(Mockery);
_tFac.Create(ref _view, ref _presenter, ref _controller);
[Test]
public void View_OnGetContentXmlButtonClick_Should_SetXmlInView()
{
//SETUP
XmlDocument xmlDocument = new XmlDocument();
using ( Mockery.Record() )
{
SetupResult.For(_view.FilePath).Return("C:\Test.txt");
Expect.Call(_bc.GetContentXml("C:\Test.txt")).Return(xmlDocument);
_view.Xml = xmlDocument.InnerXml;
}
//EXECUTE
using ( Mockery.Playback() )
{
_presenter.View_OnGetContentXmlButtonClick();
}
}