Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Mocks and Reflection
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();
}

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Green
04-08-05 01:57 PM


Re: Mocks and Reflection
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();
> }


Report this thread to moderator Post Follow-up to this message
Old Post
harris.r.boyce.iii@gmail.com
04-13-05 08:59 AM


Re: Mocks and Reflection
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


Report this thread to moderator Post Follow-up to this message
Old Post
KageVF
04-15-05 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Extreme Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:03 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.