Mocking with Rhino
We have interface that we need to mock
public interface IDataAccess
{
public List<int> GetIds(int serviceId, int standAlone, int saveChanges);
}
We want to set response as List of integers.
var listOfIds = new List<int>(){1,2,3};
Now we need to mock it
var mocks = new MockRepository();
IDataAccess _dataAccess = mocks.StrictMock<IDataAccess>();
Expect.Call(_dataAccess.GetIds(serviceId, null,null))
.IgnoreArguments()
.Return(listOfIds);
mocks.ReplayAll();
mocks.ReplayAll(); => will set up the collection ready to expect the call and return chosen results
Mocking void method
Expect.Call(()=>_className.SaveData(1, null)).IgnoreArguments();
IgnoreArguments(): Ignores all arguments passed into the method.
VerifyAll Expectations
when we have method where we pass in any parametrs and want to verify that incoming parameters are same as we expect them to be we can go about it as follows:
We will set up what do we expect to be passed into the method. And after we will check if the method has been called. When we specify parameters we need to be precise what goes into the call. If we use wrong parameters the method will never be called.
var mocks = new MockRepository();
int storeId=12;
list<items> values = new List<items>();
Expect.Call(() => _dataAccess.Store(storeId, values));
mocks.ReplayAll();
//Verification of method call
_dataAccess.VerifyAllExpectations();
No comments:
Post a Comment