It is not possible to make fun of a class with a constructor having an array parameter using Rhino Mocks

We cannot mock this class in RhinoMocks.

public class Service { public Service(Command[] commands){} } public abstract class Command {} // Code var mock = MockRepository.GenerateMock<Service>(new Command[]{}); // or mock = MockRepository.GenerateMock<Service>(null) 

Rhino mocks fails, stating that it cannot find a constructor with the appropriate arguments. What am I doing wrong?

Thanks,

+4
source share
2 answers

Try it like this:

 var mock = MockRepository.GenerateMock<Service>( new object[] { new Command[0] } ); 
+5
source

In addition, you can port Service with an interface and not worry about constructor arguments. If the constructor ever changes - your tests will be tied to these implementation details and need to be updated.

 var mock = MockRepository.GenerateMock<IService>(); 

Edit: at least isolate the creation of this Mock, so if your constructor on the service changes, you do not have to update it in every single place. Common practice is this:

(in your test class)

 private ObjectWithServiceDependency CreateObjectUnderTest(){ //Here you would inject your Service dependency with the above answer from Darin //ie var mockService= MockRepository.GenerateMock<Service>(new object[] {new Command[0] }); var objectUnderTest = new ObjectWithServiceDependency(mockService); return objectUnderTest; } 

Then in the test

 [Test] public TestSomething(){ var out = CreateObjectUnderTest(); //do testing mockService.Expect(...); } 
0
source

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


All Articles