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,

This actually make sense :)
In the test you are faking the method "Method"
But you are calling non faked method "MyTest"
This should work:
<TestMethod>
Public Sub TestMethod1()
    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)
    'call the same method that you faked
    Dim actualResult = fake.Method()
    Assert.AreEqual(5, actualResult.x)
End Sub

answered by ohad (35.4k points)
0 votes
Hi ohad.

thank you very much for answering my questions.

It really works as your last post.

However, what I really want to test is the method "MyTest" ,not "Method"
because "MyTese" calls "Method" ,but I don't want to the the real return value of "Method",So I faked the "Method".

Is my way of using Typemock wrong ?

shoud I test like this ?before I call "MyTest",I should call "Method" first ?

 Test Code 

        Dim fake As A = FakeInstance(Of A)(Members.CallOriginal) 
        'Isolator API for non public 
        NonPublicWillReturn(fake, "Method", 5) 
        fake.Method(3)
        Dim actualResult= fake.MyTest() 
        Assert.AreEqual(5, actualResult)
answered by tousinn (840 points)
0 votes
Hi,

There are few issues here:
1. What you want to do is swap the next instance of B (next call to - New B)
To do that you need to use the Isolator API SwapNextInstance(Of T)

2. From the code you posted it seems like you are using private accessors "A_Accessor" in your code. In that case you should not fake the accessor but the class that it is wrapping.

3. The Isolator can not fake fields so you can not change the value of B.x
In order to to work around that you should wrap the field with property or a method.

Combining the points above try the following code:
' I added a property to B class as a wrapper around the x field.
Public Class B
    Public x As Int32

    Public Property Prop() As Int32
        Get
            Return x
        End Get

        Set(ByVal value As Int32)
            x = value
        End Set
    End Property
End Class

<TestMethod>
Public Sub TestMethod2()
    Dim fake As A = FakeInstance(Of A)()
    Dim fakeB As B = FakeInstance(Of B)()

    Using TheseCalls.WillReturn(5)
        Dim dummy As Int32 = fakeB.Prop
    End Using
    SwapAllInstances(Of B)(fakeB)

    Dim accessor As A_Accessor = New A_Accessor()
    Dim actualResult = accessor.MyTest()
    Assert.AreEqual(5, actualResult.Prop)
End Sub


Things to note:
:arrow: I used the SwapAllInstances since the code creates few instances of B. if the code was creating only one instance I would use SwapNextInstance(Of B)(fakeB)
:arrow: the A_Accessor is not faked since I assume it only wraps A class.
answered by ohad (35.4k points)
...