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 found myself doing this so often that I added it to our unit test base class:
        protected T GetLastMockedInstance<T>()
        {
            Mock[] mocks = MockManager.GetInstanceMocks<T>();
            if (mocks.Length == 0)
                Assert.Fail("Mocked object ({0}) not found", typeof(T).ToString());

            return (T)mocks[mocks.Length - 1].MockedInstance;
        }


It would be great if you provided something like this:
myClass test = MockManager.GetLastMockedInstance<myClass>();

I know you can't call Assert.Fail, but you could throw an exception.
asked by dougclutter (3.5k points)

5 Answers

0 votes
Hi,

This is a very good suggestion. We'll put it as a candidate for future release.

Can you describe the scenarios you use the last mock?
I'd also like to get feedback from other users for this kind of API, if they will find it useful as well.
answered by gilz (14.5k points)
0 votes
I'm using it to make sure a private variable is built correctly inside a method. For example:

class myStruct { 
  string A;
  string B;
}

class bizObj {
  public string FirstName;
  public string LastName;
  public void Save() {
    myStruct x = new myStruct();
    x.A = FirstName;
    x.B = LastName;
    Stream.Write(x);
  }
}

[Test]
public void myTest() {
  using (RecordExpectations recorder = RecorderManager.StartRecording()) {
    myStruct x = new myStruct();
  }
  bizObj y = new bizObj();
  y.FirstName = "Doug";
  y.LastName = "Clutter";
  y.Save();

  myStruct z = GetLastMockedInstance<myStruct>();
  Expect( z.A == "Doug");
  Expect( z.B == "Clutter");
}
answered by dougclutter (3.5k points)
0 votes
Hi,

You are using a very interesting technique. However, it doesn't work on structs. Structs don't have instances, and therefore for mocking structs we use MockAll. However, you cannot access its mock by the recorder manager or GetInstanceMocks for valuable use.

If you substitute the struct with a class, then using with Natural Mocks, you can do this:
       
 [Test]
        public void myTest()
        {
            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                MyClass test = new MyClass();
            }
            {
                MyClass sample = new MyClass();
                sample.first = 5;
            }

            Mock mock = RecorderManager.GetLastRecordedMock();
            MyClass mockedobject = (MyClass)mock.MockedInstance;
            Assert.AreEqual(5, mockedobject .first);
       }


which will work for what you are trying to do with your example.
Let me know if this helps.
answered by gilz (14.5k points)
0 votes
Thanks for the reply, but the example I posted was extremely simplified. In my real world unit test, GetLastRecordedMock was returning the mock of a different class.

Since the class I am mocking is created inside a method, I can't be certain that the "Last Recorded Mock" will be for the myStruct class.

GetLastRecordedMock works nicely when you're only mocking one class, but I'm mocking a couple classes here, so I still feel the enhancement I requested would be helpful.

Also, please excuse the unfortunate name I selected for the class myStruct. As you can see in my sample code, myStruct is a class.

I did make a posting in the bug forum where I used myStruct as a struct...and your answer here tells me that its not a bug. Thanks for the clarification on how structs work, but it would be extremely helpful if they were returned by GetInstanceMocks.
answered by dougclutter (3.5k points)
0 votes
Thank you for the clarification, I can now understand better the kind of scenarios you're running, and what you need. At least for a single mock type, is possible to reorder the expectations that the last recording will be the one you are looking for?

We'll add the feature requests to our files, in order to implement them into a future version.

Thanks for feedback.
answered by gilz (14.5k points)
...