MSCorLib mocking fails inside Private methods

5 posts, 1 answers
  1. Joel
    Joel avatar
    19 posts
    Member since:
    Dec 2011

    Posted 04 Jan 2012 Link to this post

    Ok, this one took me a WHILE to figure out.  Not sure exactly how to explain it...

    but this scenario will fail to be mocked:
    public static bool DirectoryExists(string directory)
    {
        return DirectoryExists(new DirectoryInfo(directory));
    }
     
    private static bool DirectoryExists(DirectoryInfo directory)
    {
        bool value = Directory.Exists(directory.FullName);
        return value;
    }

    My test calls the first method, which then calls the 2nd method.

    The Directory.Exists will NOT be mocked if the method is marked as private.  If I change it to public, the mock will take place.

    I've included a sample project for your review.

    http://dl.dropbox.com/u/1006254/JustMockTestProject4.zip

    forgot to mention: I'm using MSTest
  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 06 Jan 2012 Link to this post

    Hi Joel,
    Thanks again for reporting the issue. This is a known issue with MockClassAttribute which is if you apply the attribute to FileManager class then things work as expected. Of course this an issue to fix and we are working on it.

    However, in your test class one thing I have noticed that you use Mock.Initialize on FileManager instead of Directory class. Mock.Intialize is originally meant for initializing mscorlib members and pre-intercepting members so that if invoked manually (e.g. new Foo().Submit() that invokes OnJITCompilation started and therefore cannot be intercepted in later test via profiler) does not fail the mocked called (in large tests). Therefore, you can simply write the following line in static constructor and it will suffice.
    static MSCorLibTest1()
    {
        Mock.Initialize(typeof(Directory));
    }

    Here you don’t need the extra Mock.Initialize on FileManger class. I have also attached the updated project to let you have a look.

    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. Joel
    Joel avatar
    19 posts
    Member since:
    Dec 2011

    Posted 06 Jan 2012 Link to this post

    Ok, looks like I wasn't familiar with SetupStatic and Initialize.

    I've noticed that without the [MockClass] attribute applied to the FileManager class, I still need to add this (below), or the MSCorLib_Static_Mocking_Nested_Public test will fail.

    Mock.Initialize(typeof(FileManager));

    But after adding the [MockClass] attribute to the FileManager, I can remove that line (as per your instructions).


    Though, I'm a little hesitant to add the [MockClass] attribute to the FileManager class.  In my actual project the code and the tests are in separate projects, which means I have to add a reference to JustMock in my code that will be deployed.

    Are there any concerns or issues with having references to JustMock or using the [MockClass] attribute in projects for production code?
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 10 Jan 2012 Link to this post

    Hi Joel,

    Thanks again for your reply. Yes that's an issue that you have to include MockClassAttribute to the FileManager class. This issue will hopefully be resolved in the Q1 2012 and we are working on a change that wont require you the MockClassAttribute at all.

    However, with the current release you need to include the MockClassAttribute for mocking nested mscorlib members.

    Sorry for the temporary inconvenience.


    Kind Regards,
    Mehfuz
    the Telerik team

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

  6. Joel
    Joel avatar
    19 posts
    Member since:
    Dec 2011

    Posted 10 Jan 2012 Link to this post

    Ahh okay.  Looking forward to the next release!

    Thanks!
Back to Top
DevCraft R3 2016 release webinar banner