In this post , I would be focusing on the issue that generally comes to query on how to run JustMock with standalone tools like nunit or msbuild console. Since mocking concrete method works initializing the .net profiler that is set using two variables in the runtime to let JustMock work the correct way.
COR_ENABLE_PROFILING, “0x1”
COR_PROFILER, "{D1087F67-BEE8-4f53-B27A-4E01F64F3DA8}"
Now while running msbuild or nunit console or standalone GUI tool, initialization is not always the easy / expected thing to get going. Therefore I created a tiny runner (unofficial) that will do just that and let you pipe your test DLL containing the tests with JustMock. All you have to do is create a batch file where you will pass the path to runner and test DLL in the following format:
JustMockRunner.exe "%Path to test runner%\nunit-console.exe” "%PathToDir%\JustMockTrialTests.dll"
Once the bat is executed, in this case it will open up the console, run the tests and print result from the test runner.

The runner along with a sample project (taken from a user’s post in JustMock forum) can be downloaded by clicking the link below:
P.S. To take the advantage of profiler intensive tests , JustMock full edition must be installed which actually registers the profiler. Also, please build the test project (output DLLs are removed) before running Run.bat .
Hope this helps
Update : JustMockRunner.exe to work in .net 3.5 and removed the extra Console.ReadLine that was causing problem in CI server integration.
In this post , I will show how you can mock members from MsCorlib. This is more of an introductory post and shows what you need to do in order to successfully mock an MsCorlib member.
If you are planning to mock File or DateTime then the process is pretty straight forward , you just need to put a MockClassAttribute on top your test class. The process for intercepting such is bit different as we don’t want to get your code through the particular logic for every other type and make your test slower. Therefore, we limit it to an attribute declaration to mark that the test class has some framework members to mock ahead.
In a simple example, let’s say I want to assert a DateTime:
- [TestMethod]
- public void WhenDateTimeNowIsCalledItShouldReturnMockedValue()
- {
- Mock.Arrange(() => DateTime.Now).Returns(new DateTime(1900, 2, 1));
-
- var now = DateTime.Now;
-
- Assert.AreEqual(1900, now.Year);
- }
Or Let’s say I want just want to check if File.Delete is invoked:
-
- [TestMethod]
- public void WhenFileIsDeletedItShouldInvokeTheMockedImplementation()
- {
- var filename = this.GetType().Assembly.ManifestModule.FullyQualifiedName;
- var fi = new FileInfo(filename);
-
- bool called = false;
-
- Mock.Arrange(() => fi.Delete()).DoInstead(() => called = true);
-
- fi.Delete();
-
- Assert.IsTrue(called);
- }
These require no special treatment other than the attribute declaration. But what about other types from MsCorlib and can we mock them ? Yes we can! We can mock virtually any MsCorlib type using JustMock. The only pre-requisite is to initialize it during the setup. The process is called pre-interception. It is required in a sense that you don’t have to include mock specific attribute in each test method and keep it in one place. Therefore, lets say I want to mock DriveInfo.GetDrives(). All is required that you initialize it during setup in the following way:
-
- [ClassInitialize]
- public static void Initialize(TestContext context)
- {
- Mock.Partial<DriveInfo>().For(() => DriveInfo.GetDrives());
- }
The rest is plane old vanilla:
- [TestMethod]
- public void WhenDriveInfoGetDrivesIsCalledItShouldExecuteTheMockedSetup()
- {
- bool called = false;
-
- Mock.Arrange(() => DriveInfo.GetDrives()).DoInstead(() => called = true);
-
- DriveInfo.GetDrives();
-
- Assert.IsTrue(called);
- }
You can further elevate it to initialize all its member:
- Mock.Initialize<DriveInfo>();
Hope that you find this useful. If you have any questions or ideas please directly contact the JustMock support team. Finally, this is a JustMock Commercial edition feature thus not available in the free version.
Thanks