Search code examples
c#unit-testingrhino-mockshttppostedfilebase

How do I unit test a method containing a static method?


Can this be done? Here is the method I'm testing:

public void SaveAvatarToFileSystem()
{
  Directory.CreateDirectory(AvatarDirectory);
  _file.SaveAs(FormattedFileName);
}

In my unit test, I'm using Rhino.Mocks and I want to verify that _file.SaveAs() was called. I've mocked out _file (which is an HttpPostedFileBase object) and injected it into this method's class constructor. I want to run this unit test, but not actually have the Directory.CreateDirectory call get made. Is this possible?


Solution

  • It is not possible to mock a static method using Rhino Mocks.

    However, it is possible to do this. Microsoft Moles and TypeMock have this capability.

    I have tried using Moles, and it works alright. It generates a mock assembly for you, and you are able to replace a static method at runtime with a delegate.