Home > Archive > Extreme Programming > April 2005 > Mocks and Reflection
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Mocks and Reflection
|
|
| Mark Green 2005-04-08, 8:57 am |
| 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();
}
| |
| harris.r.boyce.iii@gmail.com 2005-04-13, 3:59 am |
| Did 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();
> }
| |
| KageVF 2005-04-14, 8:57 pm |
| 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).
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
|
|
|
|
|