Is it possible to create a mock from a class that doesn't provide a no-argument constructor and don't pass any arguments to the constructor? Maybe with creating IL dynamically?
The background is that I don't want to define interfaces only for testing. The workaround would be to provide a no-argument constructor for testing.
Sure thing. In this example i'll use Moq, a really awesome mocking library.
Example:
In many cases though, I assign Mock objects as the arguments so I can test the dependencies:
It is wrong to believe that you are providing interfaces only for testing. Interfaces are there to provide abstractions and weaken the coupling between the different layers of your code making them more reusable in different contexts.
This being said the answer will depend on the mocking framework you are using. For example with Rhino Mocks you could have:
and then: