RajOpteamix / moq

Automatically exported from code.google.com/p/moq
Other
0 stars 0 forks source link

Feature Request: Allow option to use underlying object #268

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Moq is fantastic, but I'd like to be able to use it in a way it (perhaps) 
wasn't intended for - creating a wrapper to intercept object method/properties.

Given an object X of type T, I want to create a mock<T> object with behaviour 
as follows: 
If a method/property has an expectation set in the mock, use that
If no expectation is set, invoke the corresponding method/property on the 
original object X.

It's possible to do this with CastleDynamicProxy, but I love the type-safety 
expression-trees stuff in Moq. Presumably this feature would be fairly 
straightforward to implement - as an extra enum in DefaultValue?

Original issue reported on code.google.com by rob.thom...@googlemail.com on 30 Jul 2010 at 1:46

GoogleCodeExporter commented 9 years ago
what you're looking for is the CallBase property on a Mock<T>:

new Mock<Foo> { CallBase = true }

Original comment by kzu.net on 30 Jul 2010 at 5:07

GoogleCodeExporter commented 9 years ago

Original comment by kzu.net on 30 Jul 2010 at 5:07

GoogleCodeExporter commented 9 years ago
Using CallBase, it's not clear how I link the behaviour of my mocked object to 
my concrete instance X. Can you elaborate? (Apologies, I'm probably being a 
little slow)

Original comment by rob.thom...@googlemail.com on 31 Jul 2010 at 4:07

GoogleCodeExporter commented 9 years ago
The "underlying object" is always (in Moq anyway) the base class from which the 
mock of T inherits from.

You cannot pass an existing instance of T to a new Mock<T>. You get a new 
instance of it already. CallBase allows Moq to call your base class members.

Original comment by kzu.net on 2 Aug 2010 at 2:46

GoogleCodeExporter commented 9 years ago
This is the feature I requested. 
I want the behaviour of the mock<T> to default to the behaviour of X, an 
*instance* of T. This is possible using CastleDynamicProxy, but without the 
great features Moq offers.

Is this a feature that is likely to be compatible with Moq?

Original comment by rob.thom...@googlemail.com on 2 Aug 2010 at 8:45

GoogleCodeExporter commented 9 years ago
Issue 269 has been merged into this issue.

Original comment by kzu.net on 3 Aug 2010 at 2:18

GoogleCodeExporter commented 9 years ago
Now this makes perfect sense :)

Original comment by kzu.net on 3 Aug 2010 at 2:19

GoogleCodeExporter commented 9 years ago

Original comment by kzu.net on 11 Aug 2010 at 5:20

GoogleCodeExporter commented 9 years ago
Is there any progress on this feature request? Has it been implemented?

Original comment by rob.sw...@googlemail.com on 22 Mar 2012 at 12:23

GoogleCodeExporter commented 9 years ago
Nop. Good thing we moved to github (https://github.com/Moq/moq4) and anyone can 
work on it on his own fork :)

Original comment by dan...@cazzulino.com on 22 Mar 2012 at 2:37