MSCorLib Help

5 posts, 0 answers
  1. Steve
    Steve avatar
    2 posts
    Member since:
    Apr 2012

    Posted 22 Feb 2013 Link to this post

    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);            
            }
        }




  2. Steve
    Steve avatar
    2 posts
    Member since:
    Apr 2012

    Posted 22 Feb 2013 Link to this post

    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);            
            }
        }
  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 22 Feb 2013 Link to this post

    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.
  5. Steve
    Steve avatar
    13 posts
    Member since:
    Feb 2013

    Posted 22 Feb 2013 Link to this post

    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.

  6. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 25 Feb 2013 Link to this post

    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.
Back to Top
DevCraft R3 2016 release webinar banner