Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 44257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:37:26+00:00 2026-05-10T15:37:26+00:00

Has anyone been able to successfully unit test methods that are, by necessity, coupled

  • 0

Has anyone been able to successfully unit test methods that are, by necessity, coupled to the System.Windows.Forms.Form class?

I’ve recently been working on a C# winforms application, trying to build it with an MVC structure. This is difficult enough, given that the framework isn’t really built with this in mind.

However, it gets even tougher when you throw unit testing into the mix. I’ve been making sure that my controllers are not coupled to concrete view classes, so that I can use a stub/mock for unit testing. But referencing the Form class somewhere is unavoidable, and these methods do need to be tested.

I’ve been using Moq because it has some nice type-safety features, and allows mocking concrete types. But unfortunately, it doesn’t allow me to ‘expect’ calls to methods or properties on a concrete type that are neither virtual nor abstract. And since the Form class was not built with subclassing in mind, this is a big problem. I need to be able to mock the Form class to prevent real windows from being created, by ‘expecting’ ShowDialog, for example.

So I’m left unable to run any unit tests that do much interaction with subclasses of Form, which my views are.

Is there anyone out there who has successfully unit tested this type of code? How did you do it?

Is this something that other mocking frameworks can get around? Would the string-based methods used by other mocking frameworks be subject to the same constraints? Can I write my own explicit long-hand mock classes, or will the lack of virtual members prevent me from being able to suppress the window behavior that way too?

Or is there some way I haven’t thought of to structure my classes so that the Forms-coupled code ends up in methods and classes of trivial complexity, such that I can get away without explicitly unit testing them, without my conscience beating me up for it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:37:26+00:00Added an answer on May 10, 2026 at 3:37 pm

    The best method I’ve heard of/used for unit testing with GUI elements is the Humble Dialog pattern/method. In essence, the Forms are just the interface, and all the real work is done in other classes. You unit test the classes that provide the functionality, and then just tie your GUI events to the appropriate methods in those classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Vincent Danen does mention the -A option when using git… May 11, 2026 at 1:06 pm
  • added an answer The reason vim shows ((1) of 3) is because of… May 11, 2026 at 1:06 pm
  • added an answer If you really want to use a C# reserved word,… May 11, 2026 at 1:06 pm

Related Questions

Has anyone been able to succesfully use the IE CSS hack #IEroot? I stumbled
Has anyone been able to get an NHibernate-based project up and running on a
Has anyone been able to get xinc to run correctly under OpenBSD's chrooted default
Has anyone been able to get a variable record length text file (CSV) into
I'm currently building a Java app that could end up being run on many
I have a web project that I developed where one of the main functions
Okay, so basically I want to be able to retrieve keyboard text. Like entering

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.