Ishwar Jindal
unread,Nov 18, 2009, 2:55:40 AM11/18/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Rhino.Mocks
Following is the scenario
I have a model class and presenter class. Presenter calls Model to get
the data. While doing so the presenter passes a callback method to
Model. This call back method in Presenter a private method.
All I want to test is that If I mock the Model class and test
presenter by calling GetData on mocked model then I my private
callback should get called.
Following is what I have written to test this case.
public delegate void FetchEmployeeDataDelegate(int recCount);
public interface IModel : IRawModel
{
void FetchEmpData(FetchEmployeeDataDelegate callback);
void FetchEmpData(string key, FetchEmployeeDataDelegate callback);
}
public class Presenter : PresenterBase<IModel>
{
public int RecCount { get; set;}
public Presenter(IModel model) : base(model)
{
model.OnException += new EventHandler(model_OnException);
model.OnGetDetails += new EventHandler<DataArgs>
(model_OnGetDetails);
}
public void GetEmployeeDataWithKey()
{
TheModel.FetchEmpData("SomeDummyKey", OnGetEmpDataComplete);
}
public void GetEmployeeData()
{
TheModel.FetchEmpData(OnGetEmpDataComplete);
}
private void OnGetEmpDataComplete(int recCount)
{
RecCount = recCount*2;
}
}
[Test]
public void TestCallBackSimple()
{
MockRepository mocks = new MockRepository();
IModel mockModel = mocks.DynamicMock<IModel>();
int myRecCount = 30;
FetchEmployeeDataDelegate callback = delegate { };
using (mocks.Record())
{
mockModel.FetchEmpData(callback);
LastCall.Callback((Predicate<FetchEmployeeDataDelegate>)delegate
(FetchEmployeeDataDelegate callBackHandler)
{
if (callBackHandler != null)
{ callBackHandler(myRecCount);
}
return true;
});
}
using (mocks.Playback())
{
Presenter p = new Presenter(mockModel);
p.GetEmployeeData();
Assert.IsTrue(p.RecCount == (myRecCount*2));
}
}
[Test]
public void TestCallBackWithKey()
{
MockRepository mocks = new MockRepository();
IModel mockModel = mocks.DynamicMock<IModel>();
int myRecCount = 30;
FetchEmployeeDataDelegate callback = delegate { };
using (mocks.Record())
{
mockModel.FetchEmpData("SomeKey", callback);
LastCall.IgnoreArguments().Callback
((Predicate<FetchEmployeeDataDelegate>)delegate
(FetchEmployeeDataDelegate callBackHandler)
{
if (callBackHandler != null)
{ callBackHandler(myRecCount);
}
return true;
});
}
using (mocks.Playback())
{
Presenter p = new Presenter(mockModel);
p.GetEmployeeData();
Assert.IsTrue(p.RecCount == (myRecCount*2));
}
}
The test case TestCallBackSimple is working fine but
TestCallBackWithKey is throwing error saying "Callback arguments
didn't match the method arguments".
What I can conclude from this is that LastCall.Callback only works if
the method being called is taking single arameter of delegate type? Is
this correct? If yes then do we have any option in Rhino Mock to test
callback method if the caller is taking multiple parameters?
Thanks,
Ishwar