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!

"Cannot mock types from mscorlib assembly"

+1 vote
I am a newbie so these questions may be real basic. I am using the community edition so I cannot use the natural typemocks.

I am trying to mock the class File and another class StreamReader.

MockManager.Init();

Mock fMock = MockManager.Mock( typeof( File ), Constructor.StaticNotMocked );
MockObject srMock = MockManager.MockObject( typeof( StreamReader ), Constructor.Mocked, typeof( string ) );
//Mock srMock = MockManager.Mock( typeof( StreamReader ), Constructor.Mocked );
fMock.ExpectAndReturn( "OpenText", srMock.MockedInstance, typeof( string ) );

I have tried to use both the MockManager.Mock and MockManager.MockObject with StreamReader and I keep getting the error I listed in the subject line when I try to create the srMock object. What am I doing wrong?

Also, when should I use MockManager.Mock and MockManager.MockObject; what is the difference?

Thanks
asked May 28, 2007 by dalHome (310 points)

24 Answers

0 votes
Hi
Welcome to the forum.
The error you are getting is real. TypeMock can not mock types from mscorlib.
The work around around this problem is to wrap the mscorlib type in your own class and mock your class.
As for your second question:
MockManager.Mock will mock the next instance of the type
(in other words next time you use 'new')
MockManager.MockObject creates new object and mock it.
It is really usefull when you want to mock an interface or abstract class.
See more details here

Hope it is clear.
If you have more questions We'll be happy to help.
answered May 28, 2007 by ohad (37,080 points)
0 votes
The reason that mscorlib is not mocked is because other assemblies (like Code Coverage/Build System/Test Frameworks) also use these types and mocking them lead to very strange behavior.
So Files and StreamReader are tricky. The easiest way I have found to mock these are to fake the file name used to open.
eg: suppose we have the following code:
public void MyMethod()
{
   File file = File.Open(this.FileName);
}

We can mock the property
Mock myMock = MockManager.Mock<MyClass>();
myMock.ExpectGetAlways("FileName",@"..\..\dummyFileWithErrors");

This way we can keep the test files in the test project directory too.
answered May 28, 2007 by scott (29,080 points)
+1 vote
That is very disappointing :( . There are many types in mscorlib that I use and want to mock. It is a great burden to have to create a wrapper class solely for the purpose of creating a class that can be mocked. Also, creating this intermediate class is not transparent - the production code needs to reference the wrapper class rather the the "real" class.

Eliminating this need is a large part of why I would move to Typemock.

Will this feature be added?
answered May 29, 2007 by dalHome (310 points)
0 votes
Sorry that you are disapponited.
We are woking on this feature and it should be implemented in a future version.
answered May 30, 2007 by scott (29,080 points)
+1 vote
Scott,

I am looking forward to a version that supports this.

Has there been any thought to providing workarounds for this?

For example, you could provide wrapper classes in source form, or compiled in an assembly, that wrap the underlying API for all public members. Or provide a DevStudio add-in that generates a wrapper class on the fly using reflection that is added to the current open source file.

If you could automate this process it would be extremely helpful. Creating these wrappers by hand is very time consuming and error prone, and I believe that many people are confronted by this issue.

There are so many aspects to typemock that I like it's a shame that it has this limitation.
answered May 31, 2007 by dalHome (310 points)
0 votes
Hi,
We have thought of going down that path, but it is not possible to wrap most types. We are currently trying to solve the issues with mocking mscorlib types, but I am not sure when they will be released.

Out of interest, what types are you looking to mock. We might be able to mock only those types in a future version.
answered Jun 1, 2007 by scott (29,080 points)
+1 vote
Using reflector to peek into mscorlib, the classes I've used before and think would be something I might need to mock include...

Microsoft.Win32.Registry
Microsoft.Win32.RegistryKey
System.AppDomain
System.AppDomainSetup
System.Console
System.DateTime
System.IO.* almost everything here is usefully mocked. Certainly all the Stream and File classes.
System.IO.IsolatedStorage
System.Reflection.Assembly
System.Reflection.AssemblyName
System.Runtime.Remoting.*
System.Runtime.Serialization
System.Runtime.Serialization.Formatters
System.Threading

Thanks
answered Jun 1, 2007 by dalHome (310 points)
0 votes
Thanks.
answered Jun 1, 2007 by scott (29,080 points)
0 votes
Using reflector to peek into mscorlib, the classes I've used before and think would be something I might need to mock include...

Microsoft.Win32.Registry
Microsoft.Win32.RegistryKey
System.AppDomain
System.AppDomainSetup
System.Console
System.DateTime
System.IO.* almost everything here is usefully mocked. Certainly all the Stream and File classes.
System.IO.IsolatedStorage
System.Reflection.Assembly
System.Reflection.AssemblyName
System.Runtime.Remoting.*
System.Runtime.Serialization
System.Runtime.Serialization.Formatters
System.Threading

Thanks


Also, can you add the following:

System.Web.HTTPPostedFile

- Thanks!
answered Jun 13, 2007 by dagilleland (180 points)
0 votes
Hi
Thanks for the input.
We will add that to our list.
answered Jun 13, 2007 by ohad (37,080 points)
...