0

I'm attempting to get closer to 100% code coverage, and I'm interested in mocking OpenFileDialog. From some research, it appears that a good answer is to create an IFileDialogService, like this code from Open File Dialog MVVM:

public interface IOpenFileService
{
    string FileName { get; }
    bool OpenFileDialog()
    // Many other methods and properties of OpenFileDialog here...
}

However, that means that I have to implement all of the properties and methods of OpenFileDialog and simply have them be a pass-through to call the properties and methods of the real OpenFileDialog.

I was hoping to do something like having a MockContainer and a RealContainer, and each would return their version of OpenFileDialog:

public class MockContainer
{
  IOpenFileDialog FileDialog { get { return new MockOpenFileDialog(); } }
}

public class RealContainer
{
  IOpenFileDialog FileDialog { get { return new OpenFileDialog(); } }
}

However, I can't do that because they don't implement a common interface. If I was able to go with this approach, I wouldn't need to create pass-through methods in the IOpenFileService for everything needed with an OpenFileDialog. Each container would just return a dialog that the caller could use.

Is there a way I can make that approach work, or is the IOpenFileService really the way to do it?

Note: I know about mocking frameworks. I wanted to implement something quickly today, and didn't want to take the time to learn a mocking framework yet. I figured I could mock it myself fairly easily.

Community
  • 1
  • 1
Bob Horn
  • 33,387
  • 34
  • 113
  • 219

1 Answers1

1

This is a pure case for Adapter pattern

You was very close (the last step you need is inherit both MockContainer and RealContainer from some base interface - like this:

public class MockContainer : IOpenFileDialog 
{
    IOpenFileDialog FileDialog { get { return this; } }
}

public class RealContainer : IOpenFileDialog 
{
    IOpenFileDialog FileDialog { get { return this; } }
}

and then mock them as IOpenFileDialog object

Alex F
  • 3,180
  • 2
  • 28
  • 40
  • In this implementation, wouldn't I have to implement all of the OpenFileDialog methods in RealContainer? – Bob Horn Jun 17 '12 at 22:05
  • @BobHorn You don't have to. Refactor `IOpenFileDialog` to properties and functions you need. – Alex F Jun 18 '12 at 13:11
  • Right. I would need to implement all of the properties and methods that I need, and simply invoke the same property/method on a real dialog. While I was hoping to avoid having to do this, it seems like it's the only way. Thanks. – Bob Horn Jun 18 '12 at 14:44
  • The most hard part to mock `OpenFileDialog` is that this Dialog is Win32 pure window and OpenFileDialog it's just a wrapper for it. May be you could find a way but I'm searched the internet and there's no automatic/mock way to insert filename and click on 'Ok' button without help of Automation. This lead to class that wraps the Automation (FindWindow, Invoke on button, etc...). OpenFile, SaveFile and FontDialog it's hard to mock dialogs and almost always need a wrapper :( – Alex F Jun 18 '12 at 14:57