Stubbing concrete class with no default constructor

1,648 views
Skip to first unread message

TheMightyKumquat

unread,
Nov 11, 2009, 7:01:47 PM11/11/09
to Rhino.Mocks
I'm looking to confirm that the following isn't possible.

I'm trying to unit test this method:
private void ValidationCallback(object sender, ValidationEventArgs
args)
{
this._isValid = false;
this._schemaErrors += args.Message + Environment.NewLine;
}

My problem is that ValidationEventArgs has no visible constructor, so
instantiating it for the test gives a compilation error. From the
compilation error, I worked out that the constructor should have two
parameters, a XmlSchemaException and an XmlSeverityType enum value.

I wondered whether it might be possible to stub or mock the
ValidationEventArgs class, so I coded

[TestMethod]
public void ValidationCallbackTest()
{
string msg = "message";
object [] argsForConstructor = {new XmlSchemaException(),
XmlSeverityType.Warning);
ValidationEventArgs args = MockRepository.GenerateStub(typeof
(ValidationEventArgs), argsForConstructor);
Expect.Call(args.Message).Return(msg);
accessor.ValidationCallback(this, args); //accessor is a private
accessor object, instantiated when test initializes
Assert.IsFalse(accessor._isValid);
assert.AreEqual(accessor._schemaErrors, msg + Environment.NewLine);
}

This test fails at runtime with the message
"System.NotSupportedException: Parent does not have a default
constructor. The default constructor must be explicitly defined."

I take it that my stub is still trying to call the constructor of the
actual class and is being denied access, or not finding a
parameterless construcor in the concrete class? I'm a bit hazy on
this: Ayende's notes say that "Rhino Mocks supports mocking classes as
well as interfaces. In fact, it can even mock classes that don't have
a default constructor!", so I don't know why I'm getting an error
saying that I need a default constructor.

Tim Barcz

unread,
Nov 11, 2009, 7:06:32 PM11/11/09
to rhino...@googlegroups.com
The constructors are internal so subclassing is hard because you can't call base.

If this were me, I wouldn't use Rhino, I create a mock by hand to do what I want.  It's not too hard to do.
--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

TheMightyKumquat

unread,
Nov 11, 2009, 10:03:49 PM11/11/09
to Rhino.Mocks
Thanks for the reply, Tim. How would creating a mock by hand work for
a ValidationEventArgs object? Wouldn't it still run into the internal
constructor being hidden problem?

On Nov 12, 10:06 am, Tim Barcz <timba...@gmail.com> wrote:
> The constructors are internal so subclassing is hard because you can't call
> base.
>
> If this were me, I wouldn't use Rhino, I create a mock by hand to do what I
> want.  It's not too hard to do.
>
> Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz- Hide quoted text -
>
> - Show quoted text -

Tim Barcz

unread,
Nov 11, 2009, 10:24:15 PM11/11/09
to rhino...@googlegroups.com
You know I stand corrected...I looked too quickly at the class. My bad.

Thanks MS for hiding constructor....(sorry for the bad advice)
Reply all
Reply to author
Forward
0 new messages