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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm having a problem obtaining a parameter value in a DynamicReturn Value delegate function.

I am mocking a method which takes a parameter of a specific type of object and returns a generic collection; however inside the DynamicReturnValue delegate when I cast the parameter(0) object to the correct type to get hold of the parameter value the object is nothing.

At first I thought I was passing in a null/nothing reference in my NUNIT test assembly, but I'm not.

I then dicovered if I use a ParameterCheckerEx delegate in addition to the DynamicReturnValue, and in that ParameterCheckerEx function look at the parameter that is being passed to the method the parameter is correct and I can look at the correct properties for the parameter.

PLEASE NOTE: The parameter I am passing in to the DynamicReturnValue is itself a mocked object instance (created using MockObject) that has been cast (object) to the real type. I'm not sure if this has anything to do with the problem?

I could - although it's messy - use the ParameterCheckerEx delegate and store the parameter value in a member variable and then access it from the DynamicReturnValue but this is a little messy.

Does anybody know why the Parameters(0) object is not being passed in correctly to the DynamicReturnValue delegate function, and whether there is anythng else I can do from inside DynamicReturnValue to properly get hold of the real object parameter value?


Private Function MockGetErrorClassValuesParameterChecker(ByVal data As ParameterCheckerEventArgs) As Boolean
Debug.Write(data.ArgumentValue)
' Correct Value is shown!
Return True
End Function

Private Function MockGetErrorClassValues(ByVal parameters() As Object, ByVal context As Object) As Object

Dim errorClass As Kalido.Diw.RefDataSuspenseProcessing.ErrorClass = _
DirectCast(parameters(0), Kalido.Diw.RefDataSuspenseProcessing.ErrorClass)

' ErrorClass is Nothing!

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

5 Answers

0 votes
Hi
parameters(0) should give you the first argument.
It may be connected to something in the test itself.
Can you please post the test code?
answered by ohad (35.4k points)
0 votes
This isn't a straightforward scenario.

I am mocking a work-in-progress API object model that will be used by a UI layer; The UI needs the API layer but it still in development, so I have developed a Vb.NET assembly containing a set of "wrapper" mocked classes which mock the real classes (these classes set up the mocked types/mock object instances and provide mocked implementations for the set/get/method calls of the real classes;

I then have a separate NUNIT test assembly (which mimics the UI layer) which a) instantiates the mocked class assembly initially to set up a mocking infrastructures, and a set of <tests> which verify the mocking is working by instantiating the real classes and iterating through properties (so I can see the mocked implementations are being called instead of the real implementation), etc.

So I'm not sure what you need posted; I'm also not clear why the ParameterCheckerEx gets the first argument correctly, but the DynamicReturnValue does not.

However here goes:
=======================================

The problem I am having is that for a particular class which I am mocking, I am attempting to mock a method of the class that returns an ICollection; the method (ErrorRecordSet.GetErrorClasses) takes a single parameter of a specific type (Kalido.Diw.RefDataSuspenseProcessing.ErrorClass) and returns an ICollection.

NOTE: The ErrorClass parameter that I am passing in to this method has ITSELF also been mocked using MockObject - and a reference to the real object instance obtained using MockObject.Object which has been DirectCast(Kalido.Diw.RefDataSuspenseProcessing.ErrorClass). I am wondering whether this has something to do with the problem why I cannot get the object value properly? the fact that the MockObject.Object is returning an instance to the real object, but by the time the DynamicReturnValue delegate gets it, something has happened to the object?

I have set up a DynamicReturnValue delegate, and within this code I am attempting to access Parameters(0). However Parameters(0) once I've cast it is Nothing (null). If I also add a ParameterCheckerEx delegate, I can however access the correct value using Data.ArgumentValue. So I'm not clear the ParameterCheckerEx delegate is able to see the object, but when the DynamicReturnValue is called it cannot?

#1 MOCKED CLASS ASSEMBLY

' This code appears in the VB.NET assembly for the mocked version of the ErrorRecordSet parent class;

Public Class ErrorRecordSet

Public Sub New()

Dim Mock as MockManager.MockObject(Kalido.Diw.RefDataSuspenseProcessing.ErrorRecordSet)

MockManager.MockObject.AlwaysReturn("GetErrorClassValues", New DynamicReturnValue(AddressOf MockGetErrorClassValues)).Args(New ParameterCheckerEx(AddressOf MockGetErrorClassValuesParameterChecker))

End Sub

Private Function MockGetErrorClassValuesParameterChecker(ByVal data As ParameterCheckerEventArgs) As Boolean
Debug.Write(data.ArgumentValue)
' this works!
Return True
End Function

Private Function MockGetErrorClassValues(ByVal parameters() As Object, ByVal context As Object) As Object

Dim errorClass As Kalido.Diw.RefDataSuspenseProcessing.ErrorClass = _
DirectCast(parameters(0), Kalido.Diw.RefDataSuspenseProcessing.ErrorClass)
' This does not - ErrorClass is nothing!
Debug.Write(ErrorClass.Id)
End Function

End Class

#2 NUNIT TEST ASSEMBLY

' The NUNIT test code which attempts to call the mocked method:

Dim MyErrorClass As Kalido.Diw.RefDataSuspenseProcessing.ErrorClass
Debug.WriteLine(MyErrorClass.Id)
Debug.WriteLine(MyErrorClass.Name)

' Get the first errorclass for test purposes
For Each errorClass As Kalido.Diw.RefDataSuspenseProcessing.ErrorClass In m_suspenseBatchManager.GetErrorClasses(Me.TestSession)
MyErrorClass = errorClass
Exit For
Next

' Loop through collection returned by GetErrorClasses

For Each errorClassValue As Kalido.Diw.RefDataSuspenseProcessing.ErrorClassValue In errorRecordset.GetErrorClassValues(MyErrorClass)

=============================================
answered by alastair.cameron@kal (3.4k points)
0 votes
Hi
Few points:
1. The line
MockManager.MockObject.AlwaysReturn("GetErrorClassValues", New DynamicReturnValue(AddressOf MockGetErrorClassValues)).Args(New ParameterCheckerEx(AddressOf MockGetErrorClassValuesParameterChecker))

Can not compile I assume you mean
mock.AlwaysReturn("GetErrorClassValues", New DynamicReturnValue(AddressOf MockGetErrorClassValues)).Args(New ParameterCheckerEx(AddressOf MockGetErrorClassValuesParameterChecker))


2. Do you get NullReferenceException when you reference
ErrorClass.Id ?

3. Try to use the tracer and send me the output.
I'll send you my address plus instruction how to do that.
answered by ohad (35.4k points)
0 votes
#1

Yes, it was a typo.

#2

No, I don't get an error -

Dim MyErrorClass As Kalido.Diw.RefDataSuspenseProcessing.ErrorClass = DirectCast(Parameters(0), Kalido.Diw.RefDataSuspenseProcessing.ErrorClass)
Debug.WriteLine(MyErrorClass.Id)
Debug.WriteLine(MyErrorClass.Name)

both display nothing in the debug window, but I don't get an exception.

Also, the value of MyErrorClass is not Nothing (null in C#) so it contains something. And the DirectCast works so it appears to cast the parameter to the correct type - but the object just isn't behaving correctly.

#3

I enabled the TRACER and sent you the trace output last night (Monday).
answered by alastair.cameron@kal (3.4k points)
0 votes
#1

Yes, it was a typo.

#2

No, I don't get an error -

Dim MyErrorClass As Kalido.Diw.RefDataSuspenseProcessing.ErrorClass = DirectCast(Parameters(0), Kalido.Diw.RefDataSuspenseProcessing.ErrorClass)
Debug.WriteLine(MyErrorClass.Id)
Debug.WriteLine(MyErrorClass.Name)

both display nothing in the debug window, but I don't get an exception.

Also, the value of MyErrorClass is not Nothing (null in C#) so it contains something. And the DirectCast works so it appears to cast the parameter to the correct type - but the object just isn't behaving correctly.

#3

I enabled the TRACER and sent you the trace output last night (Monday).
answered by alastair.cameron@kal (3.4k points)
...