SetupAllProperties() doesn't work.

663 views
Skip to first unread message

Tobias Wilker

unread,
Mar 20, 2013, 10:27:59 AM3/20/13
to moq...@googlegroups.com
Hello,

I have a interface IContext which has some properties like IFacade. I have done the following:

Mock<IContext> context = new Mock<IContext>();
context.SetupAllProperties();

After that there is no property set. What is the problem here?
We use the version 4.0.10818.0

Regards.

Daniel Cazzulino

unread,
Mar 20, 2013, 1:59:07 PM3/20/13
to moqdisc
There is no problem there. It's the designed behavior. 

As mentioned in the quickstart https://code.google.com/p/moq/wiki/QuickStart:

"Setup a property so that it will automatically start tracking its value (also known as Stub):"

And the following bullet:

"Stub all properties on a mock (not available on Silverlight):
    
    mock.SetupAllProperties();
What were yoiu expecting it to be set to?


/kzu

--
Daniel Cazzulino | Developer Lead | Clarius Consulting | +1 425.200.4598


--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sunny

unread,
Mar 20, 2013, 1:59:54 PM3/20/13
to moq...@googlegroups.com
SetAllProperties() just prepares the properties to be able to be set to a value, and then read back.
I.e. if you do not do SetAllProperties(), when you do this:

context.Object.MyProperty = "wahtever";

when you try to read it, you will still get null, as the Get is not prepared.

If you use:
context.SetupProperty(c => c.MyProperty); //or SetAllProperties
c.Object.MyProperty = "whatever";

now

when you call the property, it'll return "wahtever".


--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Svetoslav Milenov (Sunny)

Artificial Intelligence is no match for natural stupidity.

Tobias Wilker

unread,
Mar 21, 2013, 2:29:09 AM3/21/13
to moq...@googlegroups.com
Thx, now I get what this method does. What I am looking for is a method which sets up all properties with mocks, so that in my case IFacade is also mocked and its properties also and so on.

Is there such a method?

Regards.

luke...@googlemail.com

unread,
Mar 21, 2013, 4:13:12 AM3/21/13
to moq...@googlegroups.com
Am Donnerstag, 21. März 2013 07:29:09 UTC+1 schrieb Tobias Wilker:
Thx, now I get what this method does. What I am looking for is a method which sets up all properties with mocks, so that in my case IFacade is also mocked and its properties also and so on.

Is there such a method?

Regards

Yes, there is. Just do this on the mock:

mock.DefaultValue = DefaultValue.Mock;

This setups the mock so that all properties that don't have an explicit value set return a mocked instance themselves.
Note that you don't need (or better, must not use) mock.SetupAllProperties() then.

Hope that helps.

Regards.

Tobias Wilker

unread,
Mar 21, 2013, 8:43:26 AM3/21/13
to moq...@googlegroups.com
Thx, this is exactly what I was looking for.
Reply all
Reply to author
Forward
0 new messages