Mocking objects without no-argument constructor in C# / .NET

25,071

Solution 1

Sure thing. In this example i'll use Moq, a really awesome mocking library.

Example:

public class MyObject
{
     public MyObject(object A, object B, object C)
     {
          // Assign your dependencies to whatever
     }
}

Mock<MyObject> mockObject = new Mock<MyObject>();
Mock<MyObject> mockObject = new Mock<MyObject>(null, null, null); // Pass Nulls to specific constructor arguments, or 0 if int, etc

In many cases though, I assign Mock objects as the arguments so I can test the dependencies:

Mock<Something> x = new Mock<Something>();
MyObject mockObject = new MyObject(x.Object);

x.Setup(d => d.DoSomething()).Returns(new SomethingElse());

etc

Solution 2

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:

public class Foo
{
    public Foo(string bar)
    { }

    public virtual int SomeMethod()
    {
        return 5;
    }
}

and then:

var fooMock = MockRepository.GeneratePartialMock<Foo>("abc");
fooMock.Expect(x => x.SomeMethod()).Return(10);
Share:
25,071
deamon
Author by

deamon

Updated on July 09, 2022

Comments

  • deamon
    deamon almost 2 years

    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.

  • Ritch Melton
    Ritch Melton about 13 years
    Agreed. Moq is awesome. They have a complete how-to reference on one web page. You can't be that for ease-of-use/learning.
  • deamon
    deamon about 13 years
    Does it work when the constructor requires not-null values (with code contracts or exceptions)?
  • Tejs
    Tejs about 13 years
    For example, if you have an integer, you can pass a 0. If it's a string, Null, string.Empty, etc. You can pretty much go with default(T), where T : your parameter type and everything works out fine. If you are making a mock object, methods / properties marked as virtual will never be executed; you can control what they do (via callback) or what they return (via the Return method) on the mock object.
  • PPC
    PPC almost 12 years
    Depends on your point of view: True, a developper should write interfaces to weaken coupling. False, a QA doesn't want to "improve" functional code only for testing purposes if he can skip that