Cannot mock Directory (mscorlib)

4 posts, 0 answers
  1. Brent
    Brent  avatar
    2 posts
    Member since:
    Jul 2012

    Posted 06 Jun 2013 Link to this post

    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).
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 06 Jun 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
  4. Brent
    Brent  avatar
    2 posts
    Member since:
    Jul 2012

    Posted 07 Jun 2013 Link to this post

    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
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 07 Jun 2013 Link to this post

    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.
Back to Top