Code Comments
Programming Forum and web based access to our favorite programming groups.I have a factory class (C#) which is storing ConstructorInfo instances
keyed by string. The class instantiates classes implementing an
interface using the ContructorInfo when passed the string key.
When I try to mock the interface and grab a ConstructorInfo instance
from the mock, the test fails with "Object reference not set to an
instance of an object".
Can anyone give me any ideas how I can retrieve a ConstructorInfo
froma mock object? I'm using the NUnit.Mocks namespace (v1.1.4322).
My test code is as below:
/// <summary>
/// Tests that the Execute() function is called with the correct
parameters
/// </summary>
[Test]
public void TestExecute()
{
DynamicMock mock = new DynamicMock(typeof(ISqlRunner));
mock.Expect("Execute", new object[] {"sa", string.Empty, "HR",
@"\\server\share\folder\script.sql"});
SqlRunner runner = new SqlRunner(new Hashtable());
runner.SqlRunners.Add("Oracle",
mock.MockInstance.GetType().GetConstructor(Type.EmptyTypes));
XmlDocument inputDoc = GetTestInputDocument();
runner.Execute(inputDoc);
mock.Verify();
}
Post Follow-up to this messageDid you try one of the other constructors for DynamicMock? I'm new to
TDD but I've messed around with NUnit and NMock and, with NMock, you
can do the following:
DynamicMock mock = new DynamicMock(typeof(ISqlRunner),
"MockOracleSqlRunner", typeof(object));
The second parameter, I believe, is the name of the object that is
created during the mock and the third is the base class to use if
you're mocking an interface - which you are. So the code to that would
be produced had the mock been created statically would resemble:
public class MockOracleSqlRunner : ISqlRunner
{
//code obmitted here
}
Does this help?
Harris
Mark Green wrote:
> I have a factory class (C#) which is storing ConstructorInfo
instances
> keyed by string. The class instantiates classes implementing an
> interface using the ContructorInfo when passed the string key.
>
> When I try to mock the interface and grab a ConstructorInfo instance
> from the mock, the test fails with "Object reference not set to an
> instance of an object".
>
> Can anyone give me any ideas how I can retrieve a ConstructorInfo
> froma mock object? I'm using the NUnit.Mocks namespace (v1.1.4322).
>
> My test code is as below:
>
> /// <summary>
> /// Tests that the Execute() function is called with the correct
> parameters
> /// </summary>
> [Test]
> public void TestExecute()
> {
> DynamicMock mock = new DynamicMock(typeof(ISqlRunner));
> mock.Expect("Execute", new object[] {"sa", string.Empty, "HR",
> @"\\server\share\folder\script.sql"});
>
> SqlRunner runner = new SqlRunner(new Hashtable());
> runner.SqlRunners.Add("Oracle",
> mock.MockInstance.GetType().GetConstructor(Type.EmptyTypes));
>
> XmlDocument inputDoc = GetTestInputDocument();
> runner.Execute(inputDoc);
> mock.Verify();
> }
Post Follow-up to this messageWhen I try to mock the interface and grab a ConstructorInfo instance from the mock, the test fails with "Object reference not set to an instance of an object". Can anyone give me any ideas how I can retrieve a ConstructorInfo froma mock object? I'm using the NUnit.Mocks namespace (v1.1.4322). Mark, Are you familiar with the ExpectAndReturn function? Basically, you can use that to have your Mocked Interface call return whatever you want, so you won't get the "object reference" error message. That's the short answer anyway . . . if you need more info try emailing me at kagevfNOCRAPathotmaildotcom (remove the all caps, translate the "at" and "dot", etc) Also, check out MSDN Magazine article if you haven't already: http://msdn.microsoft.com/msdnmag/i...ck/default.aspx John
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.