System.InvalidOperationException : Previous method ‘XYZ..;’ require a return value or an exception to throw. If this is haunting you, i may have the answer for that
having used NMock for a while,I have been wanting to try out Rhino Mock framework. Rhinomock is a typed library unlike other mock framworks which use string based paramaters.
However the very first time i used a rather simple implementation I was taken by surprise.
Here is the stub code. When i ran the test i got an error..
System.InvalidOperationException : Previous method ‘XYZ..;’ require a return value or an exception to throw.
Trying to figure this out took a while, onece it worked i felt really stupid. Hence this post so that someone else can save a few hours of
unwanted stress and agony
[Test] [Category("UnitTest")]
public void TestTheCallToService()
{
RequestContract request = new RequestContract();
MockRepository mocks = new MockRepository();
MyService service = new MyService();
MyServiceAgent.IAgent serviceAgent = (MyServiceAgent.IAgent)mocks.CreateMock(typeof(MyServiceAgent.IAgent));
List returnString=null ;
Expect.Call(serviceAgent.GetList()).Return(returnString);
service.GetList(request, serviceAgent);
mocks.ReplayAll();
}
Note the line mocks.ReplayAll();
This should be called before calling the service.GetList
In Rhinomock first we need to set up the expectations like any other framwork. Then we need to replay those lines from the times the recording starts . ( MockRepository mocks = new MockRepository();)
After the repay call, all the mock objects are in the correct state and the actual call to the layer, in this case a business component would actually work.
The code after the change should look like this
[Test]
[Category("UnitTest")]
public void TestTheCallToService()
{
RequestContract request = new RequestContract();
MockRepository mocks = new MockRepository();
MyService service = new MyService();
MyServiceAgent.IAgent serviceAgent = (MyServiceAgent.IAgent)mocks.CreateMock(typeof(MyServiceAgent.IAgent));
List returnString=null ;
Expect.Call(serviceAgent.GetList()).Return(returnString);
mocks.ReplayAll();
service.GetList(request, serviceAgent);
If i have saved you even a minute of time, Do let me know:)
}
Powered by ScribeFire.