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

MSCorLib mocking fails inside Private methods

4 Answers 84 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Joel asked on 05 Jan 2012, 05:13 AM
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

4 Answers, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 06 Jan 2012, 12:06 PM
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 >>

0
Joel
Top achievements
Rank 1
answered on 07 Jan 2012, 05:39 AM
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?
0
Ricky
Telerik team
answered on 10 Jan 2012, 10:41 AM
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 >>

0
Joel
Top achievements
Rank 1
answered on 10 Jan 2012, 11:02 PM
Ahh okay.  Looking forward to the next release!

Thanks!
Tags
General Discussions
Asked by
Joel
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Joel
Top achievements
Rank 1
Share this question
or