Mocking FileInfo properties

3 posts, 1 answers
  1. Chris Williams
    Chris Williams avatar
    63 posts
    Member since:
    Oct 2004

    Posted 06 Oct 2011 Link to this post

    I'm using the Trial Edition and am just getting started with JustMock.  Previously I was using Moq, and am looking at your features for mocking sealed classes, etc.

    I have a question about mocking FileInfo properties like LastWriteTimeUtc.  I saw how you mocked FileInfo methods in the example.

    Initially, I tried mocking FileSystemInfo, an abstract class, using an approach like this:

    [TestMethod]
    public void Can_Determine_Todays_File_By_LastWrite()
    {
        var mockFile = Mock.Create<FileSystemInfo>();
        Mock.Arrange(() => mockFile.FullName).Returns("c:\\MSGTRK20111001-1.log");
        Mock.Arrange(() => mockFile.Name).Returns("MSGTRK20111001-1.log");
        Mock.Arrange(() => mockFile.LastWriteTimeUtc).Returns(DateTime.UtcNow);             
           
        var result = FileRoutines.IsTodaysFile(mockFile);
        Assert.IsTrue(result);
    }

    My issue is that although the Name and FullName properties are set, LastWriteTimeUtc is not set, and retains its default value.

    I also tried mocking FileInfo by changing the constructor to look like this, but it was not effective either.  Do you have some advice?

    var mockFile = Mock.Create<FileInfo>("c:\\MSGTRK20111001-1.log");
  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 07 Oct 2011 Link to this post

    Hi Chris,

    Thanks again for reporting the issue. However, in order to mock MSCorlib types other than File, FileInfo, DateTime and HttpContext members you need to initialize it first.
     
    As an example for FileSystemInfo, you can initialize it inside a static constructor or during ClassInitailization of MSTest:
    static UnitTest1()
    {
        Mock.Partial<FileSystemInfo>().For<FileSystemInfo, DateTime>(x => x.LastWriteTimeUtc);
    }

    After this you can  mock it as usual like below (provided that you have used the MockClassAttribute on top of the test class):

    var mockFile = Mock.Create<FileSystemInfo>();
     
    Mock.Arrange(() => mockFile.FullName).Returns("c:\\MSGTRK20111001-1.log");
    Mock.Arrange(() => mockFile.Name).Returns("MSGTRK20111001-1.log");
    Mock.Arrange(() => mockFile.LastWriteTimeUtc).Returns(DateTime.UtcNow);
     
    var expected = mockFile.LastWriteTimeUtc;
     
    Assert.IsNotNull(expected);


    However, there is a bug with MockClassAttribute which is if you just move the mocked MSCorlb member to a nested class like the one you have shown:
    var result = FileRoutines.IsTodaysFile(mockFile);
     

    The test will fail but will pass if you apply MockClassAtrribute on FileRoutines class as well. We are already working on it and will release a fix for it as early as possible.

    Sorry for the inconvenience.

    Finally , if you want to get more information on mocking MSCorlib members i would recommend you to check the following online reference:

    http://www.telerik.com/help/justmock/advanced-usage-mscorlib-mocking.html

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. Chris Williams
    Chris Williams avatar
    63 posts
    Member since:
    Oct 2004

    Posted 07 Oct 2011 Link to this post

    Hi Mehfuz, thanks for your reply.

    The part that I was missing was the initialization using Mock.Partial, so I appreciate your example of that.

    I'll also be watching for the update.  As you might guess, I can't take a dependecy on Telerik.JustMock.dll in our shipping app, so I can't apply the class attribute to any non-test fixture code.

    Regards,

    Chris
Back to Top