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
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?

asked by MrClyfar (5.2k 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.

MyObject myObject = new MyObject();
ObjectState objectState = new ObjectState(myObject);
objectState.SetField("_myInt", 5);
answered by ohad (35.4k points)
0 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 by error (6.6k points)
0 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
        End Using

        SwapNextInstance(Of Person)(fakePerson)

    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?

answered by MrClyfar (5.2k points)
0 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 by dhelper (11.9k points)