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 running TypeMock 3.7.1 - in what I'm hoping is enterprise demo mode.

I try to do this:

MockManager.Init ();
Mock fileMock = MockManager.Mock(typeof(File));

I get this error:
TestCase 'ClassLibraryUnitTests.Class1Tester.GetFileDataMethod_Positive'
failed: TypeMock.TypeMockException :
*** Cannot mock types from mscorlib assembly.
<stacktrace>

Now my understanding is that this is now supported in the enterprise version of 3.7.1. Is this true? Have I somehow not installed the demo license correctly? Is there a good way to tell?


I can actually find an example in the documentation where this kind of object is mocked: https://www.typemock.com/Docs/UserGuide/ ... ments.html

That article shows this code:
MockManager.Init ();
Mock theMock = MockManager.Mock(typeof(FileStream));

Yet, when I try executing this code in my test, I get the same "Cannot mock types from mscorlib assembly." as above. This leaves me perplexed, since its straight from the docs.

Can anyone enlighten me on what TypeMock's capabilities are in this regard? It seems like an excellent tool particularly when you're adding tests to a large existing code base.


Thanks,
andy lash
asked by andylash (600 points)

2 Answers

0 votes
Hi Andy
This is mistake in the documentation.
Unfortunately TypeMock cannot mock mscorlib types

The documentation just use the FileStream class as an example for swapping
arguments.
Thanks for pointing the problem. We will fix it up
answered by ohad (35.4k points)
0 votes
Hi,
A workaround for this issue is to encpsulate the item you want to mock in a non system class.
eg. if we want to swap a file name we can do the following:
private string FileName
{
  get { return "file_to_open"; }
}


public void OpenOurFile()
{
     File.Create(FileName);
}

We can now test as follows:
[Test]
public void MyTest()
{
   Mock utilMock = MockManager.Mock(typeof(FileUtil)); 
   util.AlwaysGet("FileName","test_file_path");
}
answered by scott (32k points)
...