System.InvalidOperationException : Previous method ‘XYZ..;’ require a return value or an exception to throw.

8 Oct

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.

2 Responses to “System.InvalidOperationException : Previous method ‘XYZ..;’ require a return value or an exception to throw.”

  1. steffene October 25, 2007 at 7:37 am #

    Thanks, I just hit that problem of the same reasons as you so your hint was very helpful.

  2. Ramesh Sringeri March 9, 2010 at 1:49 pm #

    THANK YOU. U indeed did save me hours and grief. Thanks for posting this.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.