[Rhino.Mocks] System.InvalidOperationException : Can set only a single return value or exception to throw or delegate to execute on the same method call.
Today I got this exception during my unit testing and I was a bit confused because of it and the situation that I got it. I have a class that is used by another class. I also have an interface for it but in this specific case I wanted to have a partial mock in order to mock part of it functionality and in the same moment to use the other part that is a real implementation. Below is part of class and its interface.
1: public interface IDataService
2: {
3: //// some other methods here
4: void Insert(int relatedId, int userId, DataMap metaData);
5: void Delete(int id, int relatedId);
6: Guid[] GetRelatedGuids(int relatedId);
7: long GetTotalUploadedFileSize(int relatedId);
8: void GetRelatedFileRestrictions(int relatedId, out int maximumAttachmentsNumber, out int maximumAttachmentsSize);
9: }
1: public class DataService : Service, IDataService
2: {
3: //// some other methods
4:
5: public void Insert(int relatedId, int userId, DataMap metaData)
6: { /* some source code */}
7:
8: public void Delete(int id, int relatedId)
9: { /* some source code */}
10:
11: public virtual Guid[] GetRelatedGuids(int relatedId)
12: { /* some source code */}
13:
14: public long GetTotalUploadedFileSize(int relatedId)
15: { /* some source code */}
16:
17: public void GetRelatedFileRestrictions(int relatedId, out int maximumAttachmentsNumber, out int maximumAttachmentsSize)
18: { /* some source code */}
19: }
I’m testing the class that uses the interface and the implementation of the interface above. Below is part of the test method that configures the partial mock that is used for the test.
1: MockRepository mockRepository = new MockRepository();
2: IDataService service = mockRepository.PartialMock<DataService>();
3: Expect.Call(service.GetRelatedGuids(relatedId))
4: .Return(
5: new Guid[]
6: {
7: projectGuid,
8: relatedGuid
9: });
10:
11: Expect.Call(service.GetTotalUploadedFileSize(relatedId)).Return(1000);
12: int maxSize;
13: int maxNumber;
14: Expect.Call(delegate
15: {
16: service.GetRelatedFileRestrictions(relatedId, out maxNumber, out maxSize);
17: }).OutRef(3, 1024);
18:
19: mockRepository.ReplayAll();
The test is compiled and no errors are thrown. But when the test is ran the error in the post’s topic is thrown. Initially, I told that there is a problem with the parameters that are returned by the methods but it is not the case. Actually, it is caused by the configuration of the partial mock that is used. As I understood an expectation for the methods from the class that are final cannot be created and that is why I got an exception. Any methods that are virtual or abstract in a class can be overridden and consequently an expectation for them can be created. In other words cannot be mocked. I changed the definition of the methods that I wanted to have an expectation to make them virtual (in my case this is not a problem for the implementation and other related logic) and then everything started to work just fine.
Leave a Reply