RhinoMock: how to stub and return a method with a complex object as a parameter

I appreciate that someone can help me with the problem described below: I used RhinoMock in Unit Test. I define my object layout in this way, with sessionToken in lowercase form:

mockRepository.Stub(repository => repository.FindById(sessionToken)).Return(new DeviceTypeRepository().NewTable(false)); 

This is normal for the code section when calling FindById () to return a valid new new DeviceTypeRepository (). NewTable (false);

However, if a complex parameter as an object is included in the Stub, for example, a DataTable, as shown below:

 mockRepository.Stub(repository => repository.Find(sessionToken, dataTable)).Return(new DeviceTypeRepository().NewTable(false)); 

Then the section of code in which Find () is called, it does NOT return the expected new DeviceTypeRepository (). NewTable (false). Note that the input value of the dataTable parameter is the same both in the Stub call and in Find ().

Therefore, my question is: How could I implement such a parameter (typed DataTable and in general) into Stub initialization using RhinoMock? I would appreciate any advice and approaches. Thanks

+6
source share
2 answers

If it does not return what you expect, the parameters between the null call and the actual call do not match. Let's say you have something like this:

 // Set expectations var someDataTable = new DataTable(columns, raws); mockRepository .Stub(repository => repository.Find(sessionToken, dataTable)) .Return(new DeviceTypeRepository().NewTable(false)); // Actual test var anotherDataTable = new DataTable(columns, raws); yourTestObject.DoSomethingThatLooksForTheDataTable(repository); 

The fact is that even if someDataTable and anotherDataTable have the same content, they are not the same object, and when RhinoMocks compares the stub call with the actual call, the parameters do not match. What you can do is use restrictions:

 mockRepository .Stub(repository => repository.Find( Arg<SessionID>.Matches(y => y.ID == 2), Arg<DataTable>.Matches(x => x.Columns == columns && x.Raws == raws) )) .Return(true); 
+6
source

I believe that the problem is not with the complex data type, but with the expectations that you set.

As a first attempt to fix this, add IgnoreArguments() before Return . It is possible that the DataTable that you specified in the wait is different from the instance of the actually passed DataTable , so the wait will fail:

 ...Stub(...).IgnoreArguments().Return(); 

If you haven’t been helped, you can debug it manually using the “WhenCalled ():

 ...Stub(...).IgnoreArguments().WhenCalled( mi => { var token = mi.Arguments[0] as TokenDataType; var dataTable = mi.Arguments[1] as DataTable; }).Return(); 

If this does not help, try adding Repeat().Any() after Return() and see if it works. I assume that if the method was called several times, you might have missed the first return value, but I could be wrong.

+8
source

Source: https://habr.com/ru/post/899275/


All Articles