Is it possible to create different types that derive from one base type?

9 views
Skip to first unread message

Patrick

unread,
Nov 24, 2009, 2:50:03 AM11/24/09
to Moq Discussions
Hi All

I have to test a class which stores objects of different types. All
these objects need to derive from one base type. So now I'm wondering
if it is possible to create different Mock types from one base type.
So what I want to do is this:

var mockType1 = new Mock<MyBaseType>();
var mockType2 = new Mock<MyBaseType>();

Where the mockType1.Object.GetType() and mockType2.Object.GetType()
give me different types. Is this possible with Moq somehow?

Thanks

Patrick

Michael Sync

unread,
Nov 24, 2009, 3:22:28 AM11/24/09
to moq...@googlegroups.com
I think this question is as same as the one that I asked for creating child mock classes based on abstract class. I didn't get the reply for that. So, i think it's not possible with Moq. 




--
Don't go the way life takes you.
Take life the way you go

http://michaelsync.net

Andrew Kazyrevich

unread,
Nov 24, 2009, 4:14:04 AM11/24/09
to moq...@googlegroups.com
GetType is not virtual, so it's not possible to mock it.

But, for now, I see only confusion it would have brought in tests, even if it was possible.



Daniel Cazzulino

unread,
Nov 24, 2009, 4:36:20 AM11/24/09
to moq...@googlegroups.com
Indeed, GetType is not virtual.
You can simply create two custom classes (they can be abstract too) that derive from the base and use that as the type to mock. That would give you two different GetType() values.

Sometimes writing a little bit of plain old C# works like a charm ;)

I even wonder if I should remove the entire .Protected() stuff altogether... :o)

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

Patrick van der Velde

unread,
Nov 24, 2009, 4:39:47 AM11/24/09
to moq...@googlegroups.com
Hi Daniel

Yeah I figured that would be the solution. Just thought I'd ask :)

Thanks

patrick
Reply all
Reply to author
Forward
0 new messages