rgavrilov - 2005-06-23

Logged In: YES
user_id=1170022

The problem can be solved by simply defining a default
constructor on a mock object, therefore eliminating a need
for default constructor in the class to be mocked. Here is a
test fixture:

/// <summary>
/// Test mocking of class with no public default constructor.
/// </summary>
/// <author>Roman V. Gavrilov</author>
[TestFixture]
public class NoDefConstructorMockTests {
/// <summary>
/// Test class.
/// </summary>
public class NoDefConstructorClass {
public NoDefConstructorClass(object obj) {}
}
[Test]
public void NoDefConstructorClassMock() {
DynamicMockEx mock = new DynamicMockEx(typeof
(NoDefConstructorClass));
object result = mock.Object;
Assert.IsNotNull(result, "Mock object did not get
generated.");
}
}

I just added call to following method (of class
MockClassBuilder) from ClassGenerator.Generate

/// <summary>
/// Implement class's default constructor.
/// </summary>
public void ImplementDefaultConstructor() {
ConstructorBuilder cb =
_typeBuilder.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, Type.EmptyTypes);
ILGenerator il = cb.GetILGenerator();
il.Emit(OpCodes.Ret);
}

Comments:
Default constructor does not initializes any mock object's
members - but there is none anyway.