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'm trying to mock a struct...and get the mocked instance. My code is something like this:
struct myStruct { ... }

[Test]
public void myTest()
{
  using (RecordExpectations recorder = RecorderManager.StartRecording())
  {
    myStruct test = new myStruct();
  }
  myStruct sample = new myStruct();
  Mock[] mocks = MockManager.GetInstanceMocks<myStruct>();
  // mocks.Length == 0 here but I expected it to be 1.
  // If I change myStruct to a class, it works fine.
}

Is there any way to work around this?
asked by dougclutter (3.5k points)

1 Answer

0 votes
Structs do not have instaces. Rather, when trying to mock a struct, every instance is mocked. Typemock does that by using the MockAll mock mechanism. Namely, there's a single mock that is used for every use of mocking a struct of a single type.

GetInstanceMocks does not retrieve this mock by design. It retrieve instance mocks for classes. That is the reason there's no mock returned here.

Mocking a struct is possible by using MockAll<> or using the MockAllInstances property in the case of Natural mocks. It is not possible, however, to retrieve its mock following the recording. We will consider including such a feature in the future.

Please also refer to the thread on: https://www.typemock.com/community/viewtopic.php?p=2754#2754
answered by gilz (14.5k points)
...