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
in some tests of my Unit test, I want to the method to return some value that I want .

For example
 
class MyClass
{
Function MyMethod(ParameterA ) As ResultB
{}
}


the parameter "ParameterA" of the "MyMethod" is not a value object ,which is something that I defined by myself. and "ResultB" is the same.

When I use TheseCall.WillReturn like this ,
Using TheseCalls.WillReturn(l_obj_AuthResult)
        fake.MyMethod(l_obj_ParameterA)
        End Using

failed: System.ArgumentException


I don't think the Argument is wrong .

I want to know is that ok that the parameters of method are not value object when we use "TheseCalls.WillReturn" ?
asked by tousinn (840 points)

13 Answers

0 votes
Hi,

Strange, this should work of course.
Can you please send a small solution that reproduce the problem to:
support at typemock.com ?
answered by ohad (35.5k points)
0 votes
When I use that like this,
       
 Dim l_http_HttpWebResponse As HttpWebResponse = l_http_HttpWebRequest.GetResponse()
        Using TheseCalls.WillBeIgnored
            fake.GetResponse(l_http_HttpWebResponse, "20100820101155859")
        End Using


the error comes again .

Test 'AsBoxGWUT.GWBaseBLogicTest.ReceiveBusinessNexas02' failed: Test method AsBoxGWUT.GWBaseBLogicTest.ReceiveBusinessNexas02 threw exception:  TypeMock.ArrangeActAssert.NestedCallException: 
*** WhenCalled does not support using a method call as an argument.
-   To fix this pass null instead of GWBaseBLogic_Accessor.GetResponse()

***
* Example - this would work:
-   MyObj argument = Something.Other().GetStuff();
-   Isolate.WhenCalled(() => ObjUnderTest.MethodUnderTest(argument))...;
***
* Example - this would not work:
-   Isolate.WhenCalled(() => ObjUnderTest.MethodUnderTest(Something.Other().GetStuff()))...;.



could anyone tell me where I am wrong ?
answered by tousinn (840 points)
0 votes
Hi,

I'm missing a bit of context here, can you please post the full test code?
answered by ohad (35.5k points)
0 votes
thank you .
this is the test code .

        Dim fake As GWBaseBLogic_Accessor = FakeInstance(Of GWBaseBLogic_Accessor)(Members.CallOriginal)
        Dim l_obj_NXCommunicator = New NXCommunicator
        Dim l_http_HttpWebRequest As HttpWebRequest = HttpWebRequest.Create("http:\xxxx.com")
        Using TheseCalls.WillBeIgnored()
            l_obj_NXCommunicator.ResponseReceive(l_http_HttpWebRequest, "20100820101155859")
        End Using
        Dim l_xml_XmlObject = New XmlDocument
        l_xml_XmlObject.Load("Blogic\TestData\ReceiveBusinessNexas02.xml")
        Dim l_http_HttpWebResponse As HttpWebResponse = l_http_HttpWebRequest.GetResponse()
        Using TheseCalls.WillBeIgnored
            fake.GetResponse(l_http_HttpWebResponse, "20100820101155859")
        End Using

        Dim l_lst_ErrorResultList = New List(Of String)
        l_lst_ErrorResultList.Add("AccessError")
        Using TheseCalls.WillReturn(l_lst_ErrorResultList)
            fake.ResponseAnalysis(l_xml_XmlObject, "20100820101155859")
        End Using
        Dim l_obj_GWBLogicParam As New GWBLogicParam(New XmlDocument, "Param005", "20100820101155859")

        Dim l_obj_ActualResult As GWBLogicResult = fake.ReceiveBusinessNexas(l_obj_GWBLogicParam, l_http_HttpWebRequest, "1")

        Assert.AreEqual(False, l_obj_ActualResult.IsSuccess)
        Assert.AreEqual("999", l_obj_ActualResult.ErrorCd)
answered by tousinn (840 points)
0 votes
Hi,

From the code you posted I don't see any reason for the exception you got.
Can you try using the default values for the methods parameters inside the Using TheseCalls. ... blocks?
By default the parameters are ignored by the Isolator anyway.

Example:
Using TheseCalls.WillBeIgnored()
    l_obj_NXCommunicator.ResponseReceive(Nothing, Nothing)
End Using


Please let me know if solves the problem.
answered by ohad (35.5k points)
0 votes
Thank you for your answer. I am sorry I am a beginner of TypeMock Isolator
I tried to use "Nothing" as the methods parameters inside the Using TheseCalls....blocks.But it seems not to work.The same exception was thrown again.

By default the parameters are ignored by the Isolator anyway.


does this mean that I need not specify the methods parameters when I use the TheseCalls?
answered by tousinn (840 points)
0 votes
Hi,
The parameters are ignored by default. Since usually you don't care about the parameters of faked method. That means that if you write:
Using TheseCalls.WillReturn(5)
    fake.SomeMethod(String.Empty)
End Using


is the same as writing:

Using TheseCalls.WillReturn(5)
    fake.SomeMethod("abc")
End Using


Notice that the parameter of fake.SomeMethod does not change the result.

That been said you're still getting as error that you shouldn't get.
Is it possible to send a repro of the problem to our support mail?
support at typemock.com
answered by ohad (35.5k points)
0 votes
thank you very much for your answer.
I will try to send a report of the problem to your support mail.

I want to ask a new question.

you said that the parameter of fake.SomeMethod does not change the result.
But when the parameter is "ByRef",could you tell me how to deal with that ?
answered by tousinn (840 points)
0 votes
Hi,
Now I understand your first question :)
Actually it seems like you discovered a bug in the way the Isolator treats ref arguments.
Until it is fixed you can use Isolator API for non public methods as a work around.

Example:
'Production code
Public Class A
    Public Function Method(ByRef b As B) As Int32
        Return 1
    End Function
End Class

Public Class B
    Public x As Int32
End Class

'Test code
Dim fake As A = FakeInstance(Of A)()
'Isolator API for non public
NonPublicWillReturn(fake, "Method", 5)
Assert.AreEqual(5, fake.Method(Nothing))


The non public API takes the faked method name as a string and you don't need to pass the faked method arguments.

Please let me know if helps.
answered by ohad (35.5k points)
0 votes
thank you very much for your answers .
it really helps me .

your example in the last post works very well.
and It also works as well like this.

'Production code 
Public Class A
        Public Function Method(ByRef b As Integer) As Int32
            Return 1
        End Function
        Public Function MyTest() As Integer
            Dim a As Integer = 23
            Dim b As Integer = 4
            a = Method(b)
            Return a
        End Function

' Test Code

        Dim fake As A = FakeInstance(Of A)(Members.CallOriginal)
        'Isolator API for non public 
        NonPublicWillReturn(fake, "Method", 5)
        Dim actualResult= fake.MyTest()
        Assert.AreEqual(5, actualResult)



howevery ,when I test like this ,I could not get the expected value.
I turned the method of Class A to "private"
' Production Code
Public Class A
   Private Function Method(ByRef b As Net.HttpWebRequest) As B
        Dim test = New B
        test.x = 1
        Return test
    End Function
    Private Function MyTest() As B
        Dim test = New B
        test.x = 23
        Dim l_httpWebRequest As Net.HttpWebRequest = Nothing
        test = Method(l_httpWebRequest)
        Return test
    End Function
End Class


Public Class B
    Public x As Int32

End Class


' Test Code
        Dim fake As A_Accessor = FakeInstance(Of A_Accessor)(Members.CallOriginal)
        'Isolator API for non public 
        Dim test = New B
        test.x = 5
        NonPublicWillReturn(fake, "Method", test)
        Dim actualResult = fake.MyTest()
        Assert.AreEqual(5, actualResult.x)


the "Method" was called and actualResult.x is "1".
could you please tell me why ?
sorry to waste you so much time .
answered by tousinn (840 points)
...