This is a migrated thread and some comments may be shown as answers.

MSCorLib Help

4 Answers 85 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 22 Feb 2013, 04:03 PM
So, I am able to fake out static properties like DateTime.Now from the examples, but I'm having issues faking out the FileInfo objects if I create a new instance of the class in my libraries.  Can anyone help fix the code below?

// MyLibraryClass.cs
public class MyLibraryClass
{
        public void FileInfoDeleteMember(string filename)
        {
            var fi = new FileInfo(filename);
            fi.Delete();
        }
}



// FileInfoTests   
    [TestClass]
    public class FileInfoTests
    {
        static FileInfoTests()
        {
            Mock.Replace<FileInfo>(x => x.Delete()).In<MyLibraryClass>(c => c.FileInfoDeleteMember(Arg.AnyString));            
        }

        [TestMethod]
        public void ShouldNotDeleteFileWhenCalled()
        {
            var called = false;            
            var mlc = new MyLibraryClass();

            Mock.Arrange(() => new FileInfo(Arg.AnyString).Delete()).DoInstead(() => called = true);
                                   
            mlc.FileInfoDeleteMember(@"C:\test.txt");            
            Assert.AreEqual(called, true);            
        }
    }




4 Answers, 1 is accepted

Sort by
0
Steve
Top achievements
Rank 1
answered on 22 Feb 2013, 04:22 PM
I have also tried to create my own fake FileInfo class, and return it any time my Library calls 'new'.  But I get "Object reference not set to an instance of an object"


    [TestClass]
    public class FileInfoTests
    {
        static FileInfoTests()
        {
            Mock.Replace<FileInfo>(x => x.Delete()).In<MyLibraryClass>(c => c.FileInfoDeleteMember(Arg.AnyString));
            //Mock.Replace(() => new FileInfo(Arg.AnyString)).In<MyLibraryClass>(c => c.FileInfoDeleteMember(Arg.AnyString));  // obj reference not set to an instance
        }

        [TestMethod]
        public void ShouldNotDeleteFileWhenCalled()
        {
            var called = false;            
            var mlc = new MyLibraryClass();

            var fi = new FileInfo(@"C:\Test.txt");            
            Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
            //Mock.Arrange(() => new FileInfo(Arg.AnyString)).Returns(fi);  // object reference not set to an instance
                                    
            mlc.FileInfoDeleteMember(@"C:\test.txt");            
            Assert.AreEqual(called, true);            
        }
    }
0
Kaloyan
Telerik team
answered on 22 Feb 2013, 04:40 PM
Hello Steve,

Thank you for contacting Telerik support.

From your first ticket, I could see that inside the "FileInfoDeleteMember()" you are creating a new instance of FileInfo(). Having this, you will need to apply future mocking to your test method, so that you could be able to catch the new instance of FileInfo. You could read more about future mocking here. Now, the correct arrange should look like this:
Mock.Arrange(() => new FileInfo(Arg.AnyString).Delete()).IgnoreInstance().DoInstead(() => called = true);

However, while reproducing your issue, I came to an unexpected bug in JustMock. It seems that the above line could not create the correct mock for your class. We have scheduled the fixing of this issue for the near future.

As a workaround, I was able to write the following test, which is behaving as expected:
[TestMethod]
public void ShouldNotDeleteFileWhenCalled()
{
    var called = false;
    var mlc = new MyLibraryClass();
 
    var fi = new FileInfo("");
 
    Mock.Arrange(() => fi.Delete()).IgnoreArguments().IgnoreInstance().DoInstead(() => called = true);
 
    mlc.FileInfoDeleteMember(@"C:\test.txt");
    Assert.AreEqual(called, true);
}
Here, I am creating an instance of the FileInfo, with default arguments. After this, I arrange its Delete() method to "DoInstead(called = true)", while I am aplying the future mocking "IgnoreInstance()" and also arranging that, the arguments should be ignored "IgnoreArguments()".

I hope this helps. As for the fix of the original syntax, expect its implementation in the next public release of JustMock.

Please contact us again, if there is anything else we could help you with, concerning JustMock.

Regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Steve
Top achievements
Rank 1
answered on 22 Feb 2013, 05:13 PM
Thanks guys, you are always so quick to turn things around.

I really appreciate the help.

I am new to the Professional version of JustMock, so I am still getting my feet wet.

But man, it sure beats having to create an IFileInfo interface and refactor everything to support DI....especially on tons and tons of legacy code.

This tool is awesome!  Thanks again.

0
Kaloyan
Telerik team
answered on 25 Feb 2013, 08:08 AM
Hi again Steven,

I am glad that your issue is solved.

As you are new to the commercial JustMock, I would suggest to check our online help documentation, here.

Please do not hesitate to ask again, if there is anything we could help you with.

Regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
Steve
Top achievements
Rank 1
Answers by
Steve
Top achievements
Rank 1
Kaloyan
Telerik team
Steve
Top achievements
Rank 1
Share this question
or