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.
+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 by dalHome (730 points)

24 Answers

0 votes
I see previous comments by TypeMock staff as of 2007 that there was work going on to have the ability to mock certain types in mscorlib.

Has any of this been completed? If so, what version is it available in?

If not, when will it be completed?

TIA
answered by dblack (8.4k points)
0 votes
We have added the ability to fake:
DateTime.Now

And
File.ReadAllText


Currently we're addin MSCorLib class support according to users requests - is there a specific class you have in mind?
answered by dhelper (11.9k points)
0 votes
We have added the ability to fake:
DateTime.Now

And
File.ReadAllText


Currently we're addin MSCorLib class support according to users requests - is there a specific class you have in mind?


Thank you for your prompt reply :)

Specifically, it was working with mocking the IDisposable pattern. The exact functionality I was trying to mock to ensure that the method was called in my mocked class was "GC.SuppressFinalize()" and Dispose() for any resources embedded within a class that implements IDisposable.

I'm aware that this might be a very difficult type to mock because of the nature of it's purpose - but thought I'd ask and request it if possible.

BTW, in which version is the mocking of the DateTime class avilable?
answered by dblack (8.4k points)
0 votes
I'll add your request to our feature requests. The problem with faking the bahvior of GC is the possibility of breaking existing functionality - for example if you're using MemoryStream that uses GC.SUpressFinalizers it could show some wierd behavior if you manage to fake GC

We've added DateTime.Now support at 5.3.1 (25/5/2009)
answered by dhelper (11.9k points)
0 votes
I'll add your request to our feature requests. The problem with faking the bahvior of GC is the possibility of breaking existing functionality - for example if you're using MemoryStream that uses GC.SUpressFinalizers it could show some wierd behavior if you manage to fake GC

We've added DateTime.Now support at 5.3.1 (25/5/2009)


Is there any wake to Mock IDisposable types and the Disposal of their contained Disposable types (managed)?
answered by dblack (8.4k points)
0 votes
Hi

You fake types that implements IDisposable just like any other type.
Example:
public class Foo : IDisposable
{
    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

[Test]
public void Test()
{
    var fake = Isolate.Fake.Instance<Foo>();
    Isolate.Swap.NextInstance<Foo>().With(fake);

    using (var f = new Foo())
    {
    }
}


In the example above if class Foo is not faked the test will throw an exception once it exit the using block.

:arrow: You can fake interfaces and abstract types that are defined in mscorlib.

Hope it helps.
answered by ohad (35.4k points)
0 votes
Hi,

I'm currently creating and unit testing a custom ResourceProvider.

Unfortunately I cannot fake the System.Resources.ResourceManager type.

ResourceManager resourceManager = Isolate.Fake.Instance<ResourceManager>(Members.ReturnRecursiveFakes);


Can you also add this type to mscorlib supported types?
answered by nmgomes (680 points)
0 votes
Thanks - we'll add it to our list
answered by dhelper (11.9k points)
0 votes
Unfortunately I cannot fake the System.Resources.ResourceManager type.

Code:
ResourceManager resourceManager = Isolate.Fake.Instance<ResourceManager>(Members.ReturnRecursiveFakes);


Just to make this clear for everyone ... I cannot fake type directly but I can easily fake a MyResourceManager.

        private class MyResourceManager : ResourceManager
        { }

        [Isolated]
        [TestMethod()]
        public void MyTest()
        {
            [...]
            ResourceManager resourceManager = Isolate.Fake.Instance<MyResourceManager>(Members.ReturnRecursiveFakes);

            Isolate.WhenCalled(() => target.ResourceManager).WillReturn(resourceManager);
            [...]
answered by nmgomes (680 points)
0 votes
I would very like to be able to mock System.IO.File, System.IO.FileInfo, System.IO.Directory, System.IO.DirectoryInfo, and System.Threading.Task classes

This currently is a huge block to me because of the nature of the code I am trying to mock, which completely relies upon the before mentioned classes.

Thanks.
answered by ahives (180 points)
...