0

I'm coming up to speed with the Moq framework and I have a button on a form that launches another form.

The client wants the form to launch whether the Click or DoubleClick event is fired off.

My understanding of the Moq framework is that it is useful when you want to simulate certain types of actions: (For brevity, I'm only listing a few types of actions)

  1. Connect to a database
  2. Write to a file
  3. Determine network connectivity

But I'm not 100% certain on how Moq can interact with Windows controls and events.

Can anyone put me in the right direction with a simple example?

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
coson
  • 8,301
  • 15
  • 59
  • 84

1 Answers1

1

I wouldn't use Moq or unit testing for that scenario.

You have code under a visual control that launches another form. That's inherently not testable in isolation. You could automate integration tests with other tools if you wanted to.

Moq is designed for enabling unit testing. Unit testing tests functionality (often logic) in isolation. It's difficult to apply to user interfaces.

Normally what you'd do is go with one of the Model-View-Controller variants, separate your presentation layer from your business logic, use interfaces and Dependency Injection to break dependencies, then use an isolation framework such as Moq to create fake implementations of those interfaces so that you could test subsystems independently.

If this sounds complicated, it is. It's also critical to writing large, tested applications.

I'd recommend starting by reading the book The Art of Unit Testing with Examples in .NET by Roy Osherove. That covers a lot of the basics.

TrueWill
  • 25,132
  • 10
  • 101
  • 150