Telerik® JustMock™ by Progress

Telerik JustMock enables you to mock methods from the .NET Framework, i.e. from MsCorlib.

Even more, with Telerik JustMock you are now able to arrange against any MsCorlib member/function without any additional set ups. This allows you to mock MsCorlib functions/members by directly arranging their behavior.

Note

This feature is available only in the commercial version of Telerik JustMock.

Refer to this topic to learn more about the differences between both the commercial and free versions of Telerik JustMock.

Important

To use MsCorlib mocking you first need to go to elevated mode by enabling Telerik JustMock from the menu. How to Enable/Disable Telerik JustMock?

Mocking DateTime Class

The following example demonstrates how to mock the DateTime.Now method to return the date April 12, 2010.

Note that, you can now arrange the DateTime.Now without any pre-requisites.

This will also work even if the call to DateTime.Now is in member in another class. For example, take the NestedDateTime class:

In this case, the first example will look like this:

Mocking FileInfo Class

Using Telerik JustMock we can mock virtually any MsCorlib type without any pre-requisites. To demonstrate mocking of framework methods we will take the FileInfo class and mock its Delete method.

We simply arrange when fi.Delete is called to set called to true. Then, after acting we verify it.

Mocking DriveInfo Class

Let's look at another example - we want to mock DriveInfo.GetDrives():

Again, we arrange when GetDrives() is called to set called to true. Then, after acting we verify it.

Mocking File Class

The following example demonstrates how to mock the File.ReadAllBytes method.

We ignore the implementation of the method and arrange it to return our fake content for the file. After acting, we verify that both the arrays have the same length and content.

The comments in the code will guide you through the whole process:

Mocking FileStream Class

This is a more complicated example. It demonstrates how to mock the FileStream.Write method.

We arrange the Write method of the FileStream. The comments in the code will guide you through the whole process:

This is what we do here:

  1. Write "hello world" in actual.
  2. Set expectation for Write method. We replace the actual implementaion so that it will result in coping the content of actual byte array to the passed byte array.
  3. Arrange File.Open to return a custom file stream.
  4. Mimic opening a file - fileStream is assigned with the custom stream from previous step.
  5. Call the Write method. This is our original task.
  6. Finally, we assert that the both files have the same content.

See Also