Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Is there a Mock.AssignField available in AAA syntax?

+2 votes
Hi there.

I'm not sure if I've read this already on the forums, but does the new AAA syntax have an AssignField alternative, or is this something which will be introduced in a future version of the AAA syntax?

Cheers.
Jas.
asked Jan 5, 2009 by MrClyfar (1,740 points)

4 Answers

0 votes
Hi Jas

The AAA API does not have an alternative to Mock.AssignField method.
You can use the ObjectState class to do the same job.

Example:
MyObject myObject = new MyObject();
ObjectState objectState = new ObjectState(myObject);
objectState.SetField("_myInt", 5);
answered Jan 6, 2009 by ohad (37,080 points)
+2 votes
Hi Jas,

Theres a small difference between Field mocking and object state that i think you should be aware of.
Object state will allow you to change field values for a given instance,
While Field mocking will allow you to set values on a future mocked instance.
answered Jan 8, 2009 by error (4,460 points)
+2 votes
Hi there.

Many thanks for the replies.

I tried to use the ObjectState idea, but I can't seem to get it to work correctly. Here's my test code:

<TestMethod()> _
    Public Sub AnotherTest()
        Dim fakePerson As Person = FakeInstance(Of Person)()

        Dim os As New ObjectState(fakePerson)

        os.SetField("m_Age", 56)

        Using TheseCalls.WillCallOriginal
            fakePerson.GetName()
        End Using

        SwapNextInstance(Of Person)(fakePerson)

        DoSomething()
    End Sub


Here's the code for the class I'm using:

Public Class Person

    Private m_Age As Integer

    Public Function GetName() As String
        Return "Jason Evans and I'm " & m_Age & " old."
    End Function

End Class


Basically I'm just trying to see how I can mix AAA and field mocking into the same unit test. The result of running the above test is:

Jason Evans and I'm 0 old.


Would someone be able to help me out with this?

Cheers.
Jas.
answered Jan 21, 2009 by MrClyfar (1,740 points)
+2 votes
Hi Jason,

I've run your code and it worked just fine.

I think that the issue you're having is because of misunderstanding on how SwapNextINstance works - the next time you will create a Person class it would have the same behavior as the fake object but not the same values.

Instead I suggest you set behavior on GetName method.

If you need to have the exact class behavior you can use MethodRedirection to redirect the GetName call to an external class where you can set whatever values you might need.
answered Jan 21, 2009 by dhelper (23,700 points)
...