chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm trying to mock a public property of a class and replace the Get/Set with my own implementation:

VB.NET:

Public Class MyMockOfClassToBeMocked

Dim m_mockOfPropertyToBeMocked As String

Public Sub New()

MockManager.Init()

Dim mock as MockManager.Mock = MockManager.MockAll(GetType(<ClassTobeMocked>))

' Mock the Get so my member variable will be returned always as the property value

mock.ExpectGetAlways("<PropertyTobeMocked>", m_mockOfPropertyTobeMocked

' But how to mock the PropertySet???

mock.SetAlwys("<PropertyToBeMocked>") ?

End Sub

End Class
asked by alastair.cameron@kal (3.4k points)

6 Answers

0 votes
Hi
To mock property set you can use:
mock.ExpectSet("PropertyToBeMocked") to mock one call
or
mock.ExpectSetAlways("PropertyToBeMocked") to mock all calls.
answered by ohad (35.4k points)
0 votes
I'm sorry but that does not clear things up for me at all.

When a call is made by code to the set the property of the real class, a class that I am mocking all instances of using MockAll, how do I gain access to the value provided by the user?

I want to provide my own implementation of the Get/Set for the property; ExpectGetAlways will return my mocked value, but I want to intercept the property value specified when the real property is set, and use it?

That is what I don't underastand.
answered by alastair.cameron@kal (3.4k points)
0 votes
Hi
Sorry I still don't understand what you really want to do when the
property is set. I'll try to explain what are the options:

When you set a property you are really dealing with void method that gets
one argument
So
class MyClass
{
    private int x;
    public int Prop
    {
         set { x = var; }
    }
}


is like

class MyClass
{
    private int x;
    public void Prop(int n)
    {
         x = n;
    }
}

What you can do here is few things:
:arrow: Ignore the implementation
mock.ExpectSet("Prop");

:arrow: Check the arguments
mock.ExpectSet("Prop").Args(1);

This will check that that the property is set to 1. Any other number will
cause TypeMock to throw an exception.

:arrow: Do a custom arguments check with your own custom checker.
This is useful if you want to check for example that the arguments are in specific range.
mock.ExpectSet("Prop").Args(new ParameterCheckerEx(MyCostumCheckerMethod));

:arrow: All of the above plus verify that the method was called
MockManager.Verify();


If you want the property to be set to a specific value you should mock
the method that set the property

mock.ExpectAndReturn("MyMockedMethod", 5);
// ...
myClass.Prop = MyMockedMethod();


Hope it helps please tell if there is still something unclear regarding this issue.
answered by ohad (35.4k points)
0 votes
Thanks for the clarification.

I found the ParameterCheckerEx object and was playing around with it prior to your reply. It does what I want, although whether it's the best way of doing it is not clear.

What I want to do is quite simple.

============================

We are mocking some business logic classes (because the real classes are being worked on) to provide data for a UI layer (until such time as the real business logic classes are working). I have a mock VB.NET assembly that contains mocked versions of some of the real classes.

In one particular class that I am mocking (using MockAll) there is a public property (called Start) which is a datetime.

The implementation of this property in the real class simply gets or sets a member variable; now I don't really want to mock the implementation because it's fine; but what I do want to do is when the user sets the property to a specific value I want to get hold of that parameter value and store it in my own mocked version of the real class (because I need to use it do something later).

I would really like the the real property_get/property_set to function as normal, but just intercept the property set.

I tried:

.ExpectSetAlways("Start").Args(New ParameterCheckerEx(AddressOf MockStartPropertySet))

And in the delegate I simply stored the .ParameterValue and returned True to signal that the checker succeeded.

However this isn't working exactly how I want because (unless I am forced to) I'd rather the real get/set was unmocked, but the delegate was called whenever a value was specified (so I can capture it) so I'm not forced to provide my own implementation of get/set? does that make sense?

There is one thing that I didn't understand - when you said

"If you want the property to be set to a specific value you should mock
the method that set the property"

The 'method' that sets the property in the real class is the property set

public DateTime Start
{
get{ return m_startDate; }
set{ m_startDate = value; }
}

How do I mock - provide my own implementation - of a property set?

-Alastair
answered by alastair.cameron@kal (3.4k points)
0 votes
Hi,
There are a few ways to do this.
1. You could use the MockMethodCalled to get an event of the property being set
2. You could use an unmocked set and via the property checker store the value
.ExpectUnmockedSet ("Start").Args(New ParameterCheckerEx(AddressOf MockStartPropertySet))

3. You can use a dynamic return and call original
.ExpectAndReturn("set_Start", new DynamicReturnValue(AddressOf StartSet))

Public Function StartSet(ByVal parameters() As Object, ByVal context As Object) As Object 
     ' store parameters(0)

     StartSet= MockManager.CONTINUE_WITH_METHOD 
End function
Note: we used the set_Start as the name of the method, this is how properties are set. 
we return MockManager.CONTINUE_WITH_METHOD telling TypeMock to call the original method
answered by scott (32k points)
0 votes
Yes; I went with the ParameterChecker method, but the second option looks like it could be very useful for situations where I want to do something a little bit different.

Thanks for your help.
answered by alastair.cameron@kal (3.4k points)
...