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

Cannot mock Directory (mscorlib)

3 Answers 44 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brent
Top achievements
Rank 1
Brent asked on 06 Jun 2013, 05:13 AM
I cannot find a way to actually have my mock work.

Given code like this:

[Test]
public void DirectoryTest()
{
    Mock.SetupStatic(typeof (Directory), Behavior.CallOriginal, StaticConstructor.Mocked);
    Mock.Initialize(typeof (Directory)); // w/ or w/o this line - same behavior
 
 
    Mock.Arrange(() => Directory.GetDirectories(Arg.AnyString, Arg.AnyString, Arg.IsAny<SearchOption>()))
        .IgnoreArguments() // w/ or w/o this line - same behavior
        .IgnoreInstance() // w/ or w/o this line - same behavior
        .Returns(() => new[] {"somedummydirectory"});
 
    // throws ArgumentNullException here
    var test = Directory.GetDirectories(null, "*", SearchOption.TopDirectoryOnly).ToList();
    Assert.IsNotNull(test);
    Assert.AreEqual(1, test.Count);
    Assert.AreEqual("somedummydirectory", test[0]);
}


What am I missing? (using NUnit 2.6.2 with the Resharper test runner).

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 06 Jun 2013, 09:43 AM
Hello Brent,

Thank you for reaching Telerik support.

You are trying to mock a static MsCorlib member (the Directory class). Unfortunately, this is not possible in versions prior to JustMock 2013.2.522 Internal Build.

However, I suggest updating to the latest JustMock version which happens to be JustMock 2013.2.603 Internal Build. With it, you could write the test like this:
public void DirectoryTest()
{
    Mock.Arrange(() => Directory.GetDirectories(Arg.AnyString, Arg.AnyString, Arg.IsAny<SearchOption>()))
        .Returns(() => new[] { "somedummydirectory" });
 
    // throws ArgumentNullException here
    var test = Directory.GetDirectories(null, "*", SearchOption.TopDirectoryOnly).ToList();
    Assert.IsNotNull(test);
    Assert.AreEqual(1, test.Count);
    Assert.AreEqual("somedummydirectory", test[0]);
}
This will pass.

I hope this helps. Please, let me know if there is anything else I could help you with.

Regards,
Kaloyan
Telerik
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
Brent
Top achievements
Rank 1
answered on 07 Jun 2013, 07:09 AM
I've tried installing the internal build, but elevated tests all fail.  Don't know if it was 522 or 603.  Was that a known issue or resolved in 603?

When in the schedule for SP2 to be release?

Also, in your code snippet above, you're saying I won't need the Mock.SetupStatic() call?

Thanks,
Brent
0
Kaloyan
Telerik team
answered on 07 Jun 2013, 07:18 AM
Hello Brent,

The latest Internal Build is JustMock 2013.2.603. Note that, with it you will need to update the JustMock references (Telerik.JustMock.DLL) inside your project(s) to execute the tests.

We released JustMock 2013 Q1 SP2 (2013.1.507) on 07 May 2013. However, next week we will release the major JustMock 2013 Q2.

Further, you are correct that Mock.SetupStatic() is not needed in your case as you can perform partial mocking.

I hope this helps.

Regards,
Kaloyan
Telerik
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
Brent
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Brent
Top achievements
Rank 1
Share this question
or