Create a method that first creates a file and then deletes it. Sounds pretty useless but it makes a good example.

This tutorial is written in C#. You can use any mock object and unit test frameworks of your choice. In this example we're using Rhino Mocks and MbUnit v3.


  1. Create sample project.
  2. Create unit test.
  3. Create method under test.

Create sample project.

  1. Create new Class Library project and name it SysWrapSample.
  2. Add reference to your mock object framework. If you're using Rhino mocks, click on Browse tab in Add Reference dialog and reference to Rhino.Mocks.dll.
  3. Add reference to your unit test framework. If you're using MbUnit v3, click on .NET tab in Add Reference dialog and select Gallio and MbUnit.
  4. Add the last reference to SystemWrapper.dll.
  5. Rename Class1 class into FileInfoSample.
  6. Add using statements for SystemWrapper.IO, MbUnit.Framework, and Rhino.Mocks. Your class should look like this:
using SystemWrapper.IO;
using MbUnit.Framework;
using Rhino.Mocks;
namespace SysWrapSample
    public class FileInfoSample

Create unit test.

Add FileInfoSampleTest class.
public class FileInfoSampleTest
Add the test method.

public void Check_that_FileInfo_methods_Create_and_Delete_are_called()
    // Add mock repository.
    IFileInfoWrap fileInfoRepository = MockRepository.GenerateMock<IFileInfoWrap>();
    IFileStreamWrap fileStreamRepository = MockRepository.GenerateMock<IFileStreamWrap>();

    // Create expectations
    fileInfoRepository.Expect(x => x.Create()).Return(fileStreamRepository);
    fileStreamRepository.Expect(x => x.Close());
    fileInfoRepository.Expect(x => x.Delete());

    // Test
    new FileInfoSample().CreateAndDeleteFile(fileInfoRepository);

    // Verify expectations.

Create method under test.

Add CreateAndDeleteFile method to FileInfoSample class.

public void CreateAndDeleteFile(IFileInfoWrap fi)
    IFileStreamWrap fs = fi.Create();

Last edited Nov 28, 2008 at 12:33 PM by vkreynin, version 6


No comments yet.