how to reference a mock

227 views
Skip to first unread message

tecman234

unread,
Apr 21, 2011, 11:09:15 AM4/21/11
to Moq Discussions
How do I set an object's field to reference a mock?

In my example, pm.sd.Pos is a 'Position' type, which is the same type
as posmock. However, this code produces the following compile error:
Cannot implicitly convert type 'Moq.Mock<SharpTradingvs.Position>' to
'SharpTradingvs.Position' (CS0029) - C:\SharpTradingSD\Entry-Test.cs:
59,22

PositionMgr pm = new PositionMgr(sym,tflist, sqlm.Object,
ioh.Object, 1.0);
var posmock = new Mock<Position>(ioh, pm.sd, 1.0);
pm.sd.Pos = posmock;

thanks!

John Thornborrow

unread,
Apr 21, 2011, 11:54:30 AM4/21/11
to moq...@googlegroups.com
pm.sd.Pos = posmock.Object;


tecman234

unread,
Apr 23, 2011, 10:57:10 PM4/23/11
to Moq Discussions
I tried
pm.sd.Pos = posmock.Object,
but then when I run the test I get this error:
-----------
System.ArgumentException : Can not instantiate proxy of class:
SharpTradingvs.Position.
Could not find a constructor that would match given arguments:
Moq.Mock`1[SharpTradingvs.IOrderHandler]
SharpTradingvs.SymData
System.Double
---------

IOrderHandler is passed to the Position constructor, so it looks like
moq is trying to create a new Position object, instead of using the
posmock object.

Kurt.



Parameter name: constructorArguments - c:\SharpTradingSD\Entry-Test.cs:
59

On Apr 21, 10:54 am, John Thornborrow <thornborrow.j...@gmail.com>
wrote:

Daniel Cazzulino

unread,
Apr 24, 2011, 1:52:07 AM4/24/11
to moq...@googlegroups.com
Code shown is not enough.

Can you please show the ctor definition for SharpTradingvs.Position?

Mocks (like any other C# derived class from another) needs to call one of the declared constructors, and you must pass whatever values are needed to it to the Mock<T> constructor to pass them down.

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471
Reply all
Reply to author
Forward
0 new messages