TheMightyKumquat
unread,Nov 11, 2009, 7:01:47 PM11/11/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.