Nunit Mock sample for C++

251 views
Skip to first unread message

Saritha

unread,
Jun 15, 2010, 5:20:50 PM6/15/10
to NUnit-Discuss
Hi,

Is there sample test code on how to use nunit mocks in C++? I tried
the below code without success. DATE is a class within my code

Type ^dateType = DATE::typeid;
DynamicMock dateMock = gcnew DynamicMock(dateType);

error C2664: 'NUnit::Mocks::DynamicMock::DynamicMock(System::Type
^)' : cannot convert parameter 1 from 'NUnit::Mocks::DynamicMock ^' to
'System::Type ^'
No user-defined-conversion operator available, or
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

Charlie Poole

unread,
Jun 15, 2010, 5:36:07 PM6/15/10
to nunit-...@googlegroups.com
Since NUnitMocks was written as a way of testing NUnit, it really only tries to work in C#.
Theoretically, it should work in C++ as well, but I have never tried it.

Charlie


--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.


Saritha

unread,
Jun 15, 2010, 5:52:39 PM6/15/10
to NUnit-Discuss
Are there any recommendations for mock frameworks for C++ when using
NUnit?

On Jun 15, 2:36 pm, Charlie Poole <nunit...@gmail.com> wrote:
> Since NUnitMocks was written as a way of testing NUnit, it really only tries
> to work in C#.
> Theoretically, it should work in C++ as well, but I have never tried it.
>
> Charlie
>
> On Tue, Jun 15, 2010 at 2:20 PM, Saritha <sarithac...@gmail.com> wrote:
> > Hi,
>
> > Is there sample test code on how to use nunit mocks in C++? I tried
> > the below code without success. DATE is a class within my code
>
> >                        Type ^dateType = DATE::typeid;
> >                        DynamicMock dateMock = gcnew DynamicMock(dateType);
>
> > error C2664: 'NUnit::Mocks::DynamicMock::DynamicMock(System::Type
> > ^)' : cannot convert parameter 1 from 'NUnit::Mocks::DynamicMock ^' to
> > 'System::Type ^'
> > No user-defined-conversion operator available, or
> > Types pointed to are unrelated; conversion requires reinterpret_cast,
> > C-style cast or function-style cast
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "NUnit-Discuss" group.
> > To post to this group, send email to nunit-...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> > .

RJV

unread,
Jun 16, 2010, 1:28:33 AM6/16/10
to nunit-...@googlegroups.com

 Type ^dateType = DATE::typeid;
                       DynamicMock dateMock = gcnew DynamicMock(dateType);

error C2664: 'NUnit::Mocks::DynamicMock::DynamicMock(System::Type
^)' : cannot convert parameter 1 from 'NUnit::Mocks::DynamicMock ^' to
'System::Type ^'
 
Have you tried typecasting it to (Type *)?
 
DynamicMock dateMock = (Type *)gcnew DynamicMock(dateType);
 
Jv

To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.




--
Regards,

Ravichandran Jv
http://ravichandranjv.blogspot.com

Saritha

unread,
Jun 16, 2010, 12:48:24 PM6/16/10
to NUnit-Discuss
Thank you. I tried that and it compiles successfully with a warning
"warning C4669: 'type cast' : unsafe conversion:
'NUnit::Mocks::DynamicMock' is a managed type object". However when I
try to run the test it fails with the error
"System.InvalidCastException : Unable to cast object of type
'NUnit.Mocks.DynamicMock' to type 'System.Type'." at the same line.
> > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
>
> >  > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "NUnit-Discuss" group.
> > To post to this group, send email to nunit-...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nunit-discuss?hl=en.
>
> --
> Regards,
>
> Ravichandran Jvhttp://ravichandranjv.blogspot.com

RJV

unread,
Jun 17, 2010, 1:44:42 AM6/17/10
to nunit-...@googlegroups.com
When you mock test, it is usually against an interface not a class, which is what Type is. Kindly check it out with some mock framework forum like Rhino Mock.
 
Jv

To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.

Charlie Poole

unread,
Jun 17, 2010, 5:23:27 AM6/17/10
to nunit-...@googlegroups.com
Hi,

Jv is correct here. More exactly, NUnit only supports mocking either an interface or a class
that is marshalled by reference.

However, that really doesn't explain the error message, which seems to get the problem wrong.
What you need as an argument here is a Type reference (System.Type^). I am not sure if
DATE::typeid gives you that in C++. Does anyone else know?

Charlie

RJV

unread,
Jun 17, 2010, 5:40:54 AM6/17/10
to nunit-...@googlegroups.com
Yes, in C++ typeid does not give any type reference. Being a static object structure language there is no way of obtaining type info at runtime unlike in C#. A type reference is definitely required but even then it may not work because System.Type * is a class and not an interface.
 
Jv

Markus Ewald

unread,
Jun 17, 2010, 7:19:47 AM6/17/10
to nunit-...@googlegroups.com
On 6/15/2010 11:20 PM, Saritha wrote:
> Hi,
>
> Is there sample test code on how to use nunit mocks in C++? I tried
> the below code without success. DATE is a class within my code
>
> Type ^dateType = DATE::typeid;
> DynamicMock dateMock = gcnew DynamicMock(dateType);
>
>
You are assigning a managed pointer (DynamicMock ^) to a value
(DynamicMock).

This works for me:

Type ^dateType = DateTime::typeid;
DynamicMock ^dynamicMock = gcnew DynamicMock(dateType);

To my knowledge, there shouldn't be anything C# can do that wouldn't map
to C++/CLI in one way or another.

-Markus-

RJV

unread,
Jun 17, 2010, 8:32:52 AM6/17/10
to nunit-...@googlegroups.com
C++ is an unmanaged environment whereas C# works in the managed env. That is the reason why the keyword "gcnew" is used instead of "new" for object reference. The usage of pointer can be used in C# only in an "unsafe" context, which is again the reason why the default "new" keyword of C# is not being used here so that the memory reference for an unsafe type is not created in the managed heap. By using the "gcnew" keyword, the CLR knows now the context under which the C++ pointer should be managed.
 
Anyway, this is not the point at all. The point is Date::typeid is much like how you get runtime info with RTTI in VC++. It is just a keyword and not a class that falls under the System.Object class of .Net and hence, is not recognized as a type at all by the CLR.
If instead of
 
                       Type ^dateType = DATE::typeid;
if you use
 
                       Type ^dateType = <some_known_primitive_type_like_string>;
 
I haven't tried it because I do not have a C++ compiler installed in my machine but it should work.
 
Jv
--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.

Charlie Poole

unread,
Jun 17, 2010, 11:22:22 AM6/17/10
to nunit-...@googlegroups.com
Hi Markus,

Yes... I imagine that the problem is with DATE. It's a class in your app and we don't have its
definition available, but it most likely does not meet the requirement of being an MBR class,
which is the stated limitation of DynamicMock.

Charlie


--

Charlie Poole

unread,
Jun 17, 2010, 11:28:35 AM6/17/10
to nunit-...@googlegroups.com
Hi JV,

I commented on this in an earlier note and it seems you didn't notice. Please excuse
my being quite direct this time.

DynamicMock does _not_ require an interface. It may be used with any Type for which
a proxy can be constructed. This includes interfaces as well as MBR classes.

In any case, it's not the System.Type that causes the problem but the class being mocked,
DATE in this case.

Charlie

Charlie Poole

unread,
Jun 17, 2010, 11:30:38 AM6/17/10
to nunit-...@googlegroups.com
Hi All,

Actually, we have no samples for use of DynamicMock, in any language at all.

Does someone want to propose a sample? It should be translated or translatable
into C#, VB and C++/CLI.

Charlie

Saritha

unread,
Jun 17, 2010, 2:30:24 PM6/17/10
to NUnit-Discuss
Charlie - Could you explain or point me to a reference on MBR class?
what does it mean? Nunit Mock samples in C++/CLI would greatly help!

Thanks!
Saritha.

On Jun 17, 8:30 am, Charlie Poole <nunit...@gmail.com> wrote:
> Hi All,
>
> Actually, we have no samples for use of DynamicMock, in any language at all.
>
> Does someone want to propose a sample? It should be translated or
> translatable
> into C#, VB and C++/CLI.
>
> Charlie
>
> On Thu, Jun 17, 2010 at 8:28 AM, Charlie Poole <nunit...@gmail.com> wrote:
> > Hi JV,
>
> > I commented on this in an earlier note and it seems you didn't notice.
> > Please excuse
> > my being quite direct this time.
>
> > DynamicMock does _not_ require an interface. It may be used with any Type
> > for which
> > a proxy can be constructed. This includes interfaces as well as MBR
> > classes.
>
> > In any case, it's not the System.Type that causes the problem but the class
> > being mocked,
> > DATE in this case.
>
> > Charlie
>
> > On Thu, Jun 17, 2010 at 2:40 AM, RJV <jv.ravichand...@gmail.com> wrote:
>
> >> Yes, in C++ typeid does not give any type reference. Being a static object
> >> structure language there is no way of obtaining type info at runtime unlike
> >> in C#. A type reference is definitely required but even then it may not work
> >> because System.Type * is a class and not an interface.
>
> >> Jv
> >> On Thu, Jun 17, 2010 at 2:53 PM, Charlie Poole <nunit...@gmail.com>wrote:
>
> >>> Hi,
>
> >>> Jv is correct here. More exactly, NUnit only supports mocking either an
> >>> interface or a class
> >>> that is marshalled by reference.
>
> >>> However, that really doesn't explain the error message, which seems to
> >>> get the problem wrong.
> >>> What you need as an argument here is a Type reference (System.Type^). I
> >>> am not sure if
> >>> DATE::typeid gives you that in C++. Does anyone else know?
>
> >>> Charlie
>
> >>>   On Wed, Jun 16, 2010 at 10:44 PM, RJV <jv.ravichand...@gmail.com>wrote:
>
> >>>>   When you mock test, it is usually against an interface not a class,
> >>>> which is what Type is. Kindly check it out with some mock framework forum
> >>>> like Rhino Mock.
>
> >>>> Jv
>
> >>>>> > > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
> >>>>> <nunit-discuss%252Buns...@googlegroups.com<nunit-discuss%25252Bun...@googlegroups.com>
>
> >>>>> > >  > > .
> >>>>> > > > > For more options, visit this group at
> >>>>> > > > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>>>> > > --
> >>>>> > > You received this message because you are subscribed to the Google
> >>>>> Groups
> >>>>> > > "NUnit-Discuss" group.
> >>>>> > > To post to this group, send email to
> >>>>> nunit-...@googlegroups.com.
>
> >>>>> > > To unsubscribe from this group, send email to
> >>>>> > > nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>>>> <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>>
>
> >>>>> > > .
> >>>>> > > For more options, visit this group at
> >>>>> > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>>>> > --
> >>>>> > Regards,
>
> >>>>> > Ravichandran Jvhttp://ravichandranjv.blogspot.com
>
> >>>>> --
> >>>>>  You received this message because you are subscribed to the Google
> >>>>> Groups "NUnit-Discuss" group.
> >>>>> To post to this group, send email to nunit-...@googlegroups.com.
> >>>>> To unsubscribe from this group, send email to
> >>>>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>>>> .
> >>>>> For more options, visit this group at
> >>>>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>>> --
> >>>> Regards,
>
> >>>> Ravichandran Jv
> >>>>http://ravichandranjv.blogspot.com
>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "NUnit-Discuss" group.
> >>>> To post to this group, send email to nunit-...@googlegroups.com.
> >>>> To unsubscribe from this group, send email to
> >>>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>>> .
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "NUnit-Discuss" group.
> >>> To post to this group, send email to nunit-...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> >> --
> >> Regards,
>
> >> Ravichandran Jv
> >>http://ravichandranjv.blogspot.com
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "NUnit-Discuss" group.
> >> To post to this group, send email to nunit-...@googlegroups.com.
> >> To unsubscribe from this group, send email to

Saritha

unread,
Jun 17, 2010, 2:38:46 PM6/17/10
to NUnit-Discuss
Charlie - Could you explain or point me to a reference on MBR class?
what does it mean? Nunit Mock samples in C++/CLI would greatly help!

Thanks!
Saritha.

On Jun 17, 8:30 am, Charlie Poole <nunit...@gmail.com> wrote:
> Hi All,
>
> Actually, we have no samples for use of DynamicMock, in any language at all.
>
> Does someone want to propose a sample? It should be translated or
> translatable
> into C#, VB and C++/CLI.
>
> Charlie
>
> On Thu, Jun 17, 2010 at 8:28 AM, Charlie Poole <nunit...@gmail.com> wrote:
> > Hi JV,
>
> > I commented on this in an earlier note and it seems you didn't notice.
> > Please excuse
> > my being quite direct this time.
>
> > DynamicMock does _not_ require an interface. It may be used with any Type
> > for which
> > a proxy can be constructed. This includes interfaces as well as MBR
> > classes.
>
> > In any case, it's not the System.Type that causes the problem but the class
> > being mocked,
> > DATE in this case.
>
> > Charlie
>
> > On Thu, Jun 17, 2010 at 2:40 AM, RJV <jv.ravichand...@gmail.com> wrote:
>
> >> Yes, in C++ typeid does not give any type reference. Being a static object
> >> structure language there is no way of obtaining type info at runtime unlike
> >> in C#. A type reference is definitely required but even then it may not work
> >> because System.Type * is a class and not an interface.
>
> >> Jv
> >> On Thu, Jun 17, 2010 at 2:53 PM, Charlie Poole <nunit...@gmail.com>wrote:
>
> >>> Hi,
>
> >>> Jv is correct here. More exactly, NUnit only supports mocking either an
> >>> interface or a class
> >>> that is marshalled by reference.
>
> >>> However, that really doesn't explain the error message, which seems to
> >>> get the problem wrong.
> >>> What you need as an argument here is a Type reference (System.Type^). I
> >>> am not sure if
> >>> DATE::typeid gives you that in C++. Does anyone else know?
>
> >>> Charlie
>
> >>>   On Wed, Jun 16, 2010 at 10:44 PM, RJV <jv.ravichand...@gmail.com>wrote:
>
> >>>>   When you mock test, it is usually against an interface not a class,
> >>>> which is what Type is. Kindly check it out with some mock framework forum
> >>>> like Rhino Mock.
>
> >>>> Jv
>
> >>>>> > > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
> >>>>> <nunit-discuss%252Buns...@googlegroups.com<nunit-discuss%25252Bun...@googlegroups.com>
>
> >>>>> > >  > > .
> >>>>> > > > > For more options, visit this group at
> >>>>> > > > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>>>> > > --
> >>>>> > > You received this message because you are subscribed to the Google
> >>>>> Groups
> >>>>> > > "NUnit-Discuss" group.
> >>>>> > > To post to this group, send email to
> >>>>> nunit-...@googlegroups.com.
>
> >>>>> > > To unsubscribe from this group, send email to
> >>>>> > > nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>>>> <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>>
>
> >>>>> > > .
> >>>>> > > For more options, visit this group at
> >>>>> > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>>>> > --
> >>>>> > Regards,
>
> >>>>> > Ravichandran Jvhttp://ravichandranjv.blogspot.com
>
> >>>>> --
> >>>>>  You received this message because you are subscribed to the Google
> >>>>> Groups "NUnit-Discuss" group.
> >>>>> To post to this group, send email to nunit-...@googlegroups.com.
> >>>>> To unsubscribe from this group, send email to
> >>>>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>>>> .
> >>>>> For more options, visit this group at
> >>>>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>>> --
> >>>> Regards,
>
> >>>> Ravichandran Jv
> >>>>http://ravichandranjv.blogspot.com
>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "NUnit-Discuss" group.
> >>>> To post to this group, send email to nunit-...@googlegroups.com.
> >>>> To unsubscribe from this group, send email to
> >>>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>>> .
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "NUnit-Discuss" group.
> >>> To post to this group, send email to nunit-...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> >> --
> >> Regards,
>
> >> Ravichandran Jv
> >>http://ravichandranjv.blogspot.com
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "NUnit-Discuss" group.
> >> To post to this group, send email to nunit-...@googlegroups.com.
> >> To unsubscribe from this group, send email to

Saritha

unread,
Jun 17, 2010, 2:39:18 PM6/17/10
to NUnit-Discuss
Hi Markus,

Thank you. That did work. However my code threw an error at the last
line

Type ^dateType = DATE::typeid;
DynamicMock ^dateMock = gcnew DynamicMock(dateType);

String ^methodName="split";
dateMock->ExpectAndReturn(methodName, false);

int result;
result=actual_days_between_dates(%(*dateMock->MockInstance),%
(*dateMock->MockInstance));

The error was "error C2664: 'actual_days_between_dates' : cannot
convert parameter 1 from 'System::Object ^' to 'const DATE *' No user-
defined-conversion operator available, or Cannot convert a managed
type to an unmanaged type"

the method I'm testing is "int actual_days_between_dates(const DATE
*start_date, const DATE *end_date)"

-Saritha.

Charlie Poole

unread,
Jun 17, 2010, 3:37:38 PM6/17/10
to nunit-...@googlegroups.com
Hi Saritha,

See http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx

Note that you should _not_ change your type to use marshal by reference
simply to allow mocking it. Use the appropriate type of marshalling (or no
marshalling) called for in the application design. Create an interface if
necessary to do your mocking.

Charlie

To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.

Saritha

unread,
Jun 17, 2010, 7:08:24 PM6/17/10
to NUnit-Discuss
Hi Charlie,

Thanks! When referring to interfaces in C++ does Nunit Mock equate
that to an abstract class i.e. a class with virtual functions in it? I
don't believe an interface in C# has an equivalent in C++.

-Saritha.

On Jun 17, 12:37 pm, Charlie Poole <nunit...@gmail.com> wrote:
> Hi Saritha,
>
> Seehttp://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.aspx
>
> Note that you should _not_ change your type to use marshal by reference
> simply to allow mocking it. Use the appropriate type of marshalling (or no
> marshalling) called for in the application design. Create an interface if
> necessary to do your mocking.
>
> Charlie
>
> > > >>>>> > > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
> > <nunit-discuss%252Buns...@googlegroups.com<nunit-discuss%25252Bun...@googlegroups.com>
>
> > > >>>>> <nunit-discuss%252Buns...@googlegroups.com<nunit-discuss%25252Bun...@googlegroups.com>
> > <nunit-discuss%25252Bun...@googlegroups.com<nunit-discuss%2525252Bu...@googlegroups.com>
>
> > > >>>>> > >  > > .
> > > >>>>> > > > > For more options, visit this group at
> > > >>>>> > > > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> > > >>>>> > > --
> > > >>>>> > > You received this message because you are subscribed to the
> > Google
> > > >>>>> Groups
> > > >>>>> > > "NUnit-Discuss" group.
> > > >>>>> > > To post to this group, send email to
> > > >>>>> nunit-...@googlegroups.com.
>
> > > >>>>> > > To unsubscribe from this group, send email to
> > > >>>>> > > nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
>
> > > >>>>> <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
> > <nunit-discuss%252Buns...@googlegroups.com<nunit-discuss%25252Bun...@googlegroups.com>
>
> > > >>>>> > > .
> > > >>>>> > > For more options, visit this group at
> > > >>>>> > >http://groups.google.com/group/nunit-discuss?hl=en.
>
> > > >>>>> > --
> > > >>>>> > Regards,
>
> > > >>>>> > Ravichandran Jvhttp://ravichandranjv.blogspot.com
>
> > > >>>>> --
> > > >>>>>  You received this message because you are subscribed to the Google
> > > >>>>> Groups "NUnit-Discuss" group.
> > > >>>>> To post to this group, send email to
> > nunit-...@googlegroups.com.
> > > >>>>> To unsubscribe from this group, send email to
> > > >>>>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
>
> > > >>>>> .
> > > >>>>> For more options, visit this group at
> > > >>>>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> > > >>>> --
> > > >>>> Regards,
>
> > > >>>> Ravichandran Jv
> > > >>>>http://ravichandranjv.blogspot.com
>
> > > >>>> --
> > > >>>> You received this message because you are subscribed to the Google
> > > >>>> Groups "NUnit-Discuss" group.
> > > >>>> To post to this group, send email to nunit-...@googlegroups.com
> > .
> > > >>>> To unsubscribe from this group, send email to
> > > >>>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
>
> > > >>>> .
> > > >>>> For more options, visit this group at
> > > >>>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> > > >>> --
> > > >>> You received this message because you are subscribed to the Google
> > Groups
> > > >>> "NUnit-Discuss" group.
> > > >>> To post to this group, send email to nunit-...@googlegroups.com.
> > > >>> To unsubscribe from this group, send email to
> > > >>> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
> > <nunit-discuss%2Bunsu...@googlegroups.com<nunit-discuss%252Buns...@googlegroups.com>
>
> > > >>> .
> > > >>> For more options, visit this group at
> > > >>>http://groups.google.com/group/nunit-discuss?hl=en.
>
> > > >> --
> > > >> Regards,
>
> > > >> Ravichandran Jv
> > > >>http://ravichandranjv.blogspot.com
>
> > > >> --
> > > >> You received this message because you are subscribed to the Google
> > Groups
> > > >> "NUnit-Discuss" group.
> > > >> To post to this group, send email to nunit-...@googlegroups.com.
> > > >> To unsubscribe from this group, send email to
> > > >> nunit-discus...@googlegroups.com<nunit-discuss%2Bunsu...@googlegroups.com>
>
> ...
>
> read more »

Markus Ewald

unread,
Jun 17, 2010, 8:00:19 PM6/17/10
to nunit-...@googlegroups.com
On 6/17/2010 8:39 PM, Saritha wrote:
Hi Markus,

Thank you. That did work. However my code threw an error at the last
line

			Type ^dateType = DATE::typeid;
			DynamicMock ^dateMock = gcnew DynamicMock(dateType);

			String ^methodName="split";
			dateMock->ExpectAndReturn(methodName, false);

			int result;
			result=actual_days_between_dates(%(*dateMock->MockInstance),%
(*dateMock->MockInstance));

The error was "error C2664: 'actual_days_between_dates' : cannot
convert parameter 1 from 'System::Object ^' to 'const DATE *'  No user-
defined-conversion operator available, or  Cannot convert a managed
type to an unmanaged type"

the method I'm testing is "int actual_days_between_dates(const DATE
*start_date, const DATE *end_date)"
  

Sorry, until this point I assumed your DATE would be a managed type. If DATE is an unmanaged type, all that will probably not work.

C++/CLI will generate a placeholder reflection type for native types so managed code can identify native types, but that's pretty all it can do with it.
Run this code snippet and notice how .NET will be not able to see the split() method in the DATE class.
class DATE
{
    public: virtual int split() const {}
};

int main(array<System::String ^> ^args)
{

    Type ^dateType = DATE::typeid;

    Console::WriteLine(dateType->ToString());

    array<MethodInfo ^> ^methodInfos = dateType->GetMethods();
    for(int index = 0; index < methodInfos->Length; ++index) {
        Console::WriteLine(methodInfos[index]->ToString());
    }

    return 0;
}
To dynamically mock C++ classes, I believe you will need to either manually write a mock or find a C++ mock library. I'm not aware of any such libraries, but I haven't done much testing in C++ at all. It's not impossible to do (take method pointer in C++, offset pointer into a vtable, copy vtable, override method), so I'd expect someone should already have built a library to do this out there.

-Saritha.

  
-Markus-

Markus Ewald

unread,
Jun 17, 2010, 8:05:12 PM6/17/10
to nunit-...@googlegroups.com
On 6/18/2010 2:00 AM, I wrote:
To dynamically mock C++ classes, I believe you will need to either manually write a mock or find a C++ mock library. I'm not aware of any such libraries, but I haven't done much testing in C++ at all. It's not impossible to do (take method pointer in C++, offset pointer into a vtable, copy vtable, override method), so I'd expect someone should already have built a library to do this out there.

One more possibility: Wrap your C++ type in a managed class with C++/CLI so it can be used via reflection.
But not maintenance-friendly and pretty boring to do. Could be automated with a good C++ code parser (yeah, sure :P).

-Markus-

Charlie Poole

unread,
Jun 17, 2010, 9:16:04 PM6/17/10
to nunit-...@googlegroups.com
It would have to be a managed interface or a (managed) mbr class. Unmanaged
C++ types don't qualify. For C++ mocking, you may want to look at Google mocks.

Charlie

To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages