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

Mocking FileInfo properties

2 Answers 434 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris Williams
Top achievements
Rank 1
Chris Williams asked on 06 Oct 2011, 10:57 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 07 Oct 2011, 06:57 PM
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 >>

0
Chris Williams
Top achievements
Rank 1
answered on 07 Oct 2011, 07:42 PM
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
Tags
General Discussions
Asked by
Chris Williams
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Chris Williams
Top achievements
Rank 1
Share this question
or