2

Hello I'm trying to make a unit test for a class which constructor looks like this:

public Background(Form mainForm, Form optionMenu, bool startMinimized, string server)

So in my unit test I will need to have access to a windows Form of some sort (Instantiate a new one or access an existing one.)

But as I can see there is no way to use windows Forms in unit testing (Unless I have missed something)

anyway to come around this without creating a second constructor for the unit test?

Son_Of_Diablo
  • 89
  • 3
  • 12

2 Answers2

2

I'm not sure that what you're doing is a unit test, because if your test needs to instantiate a Window, then you're also testing the framework as well.

If, for example, you're running your tests on a Build machine that due to a lightning strike fails to create .net WinForms forms, the test will fail, even though your code works perfectly.

If possible, try to separate the dependencies, and use some kind of Mocking technique (hand-written, or with a framework such as MoQ or RhinoMocks).

Read more about it in Roy Oshorov's blog.

UPDATE: After reading your comment (1):

You said that you're testing a "Notify Icon" class - it's something that is very hard to unit test without being dependent on Windows. I would consider abstracting it from your real business logic (I would consider creating a system-wide service, an INotificationService or something), and test how your business logic code reacts with this service - you can easily place a mocked service and test your code without bashing heads with the .net Framework.

UPDATE: After reading your comment (2):

Regarding the comment "I'm going to test a ping function inside Background which only need the string server" - Can you pass null to the constructor? (could throw an exception), or perhaps just new Form()? (This technique is called faking - providing irrelevant objects just to have the arrangement step of the test pass without errors - see here for more details)

Community
  • 1
  • 1
Felix
  • 1,034
  • 1
  • 9
  • 29
  • I just need a windows Form object to pass to the class so I can instantiate the class in the unit test. I'm just trying to find out if it's possible or if I need to make a second constructor for the class to use in my unit test – Son_Of_Diablo Sep 10 '14 at 11:12
  • to your update: This is a school project and we have some guide lines to follow, so I can't really do what you are proposing... – Son_Of_Diablo Sep 10 '14 at 11:19
  • In that case, consult with your teacher. What is the purpose of the project? Perhaps doing UnitTests is an overkill... – Felix Sep 10 '14 at 11:49
  • I can't consult my teacher until Friday and I need it done today -.-'' The purpose is to learn to use Unified Process and to make white and black box testing.. – Son_Of_Diablo Sep 10 '14 at 11:51
  • @UPDATE 2 I can't pass null as it's not a valid form.. and I can't make a new form in the Unit Test as I can't use System.Windows.Forms form the unit test – Son_Of_Diablo Sep 10 '14 at 18:28
  • Well, now you know the pains of code not being unit-testable. Industry standard would suggest hiding the dependency under an interface (have your window implement IPing or something) and test against some mocked, stubbed or faked IPing implementation. – Felix Sep 11 '14 at 11:18
1

Okay I found out how to fix my problem..

I needed to add a reference to System.Windows.Forms

Then I could use Using System.Windows.Forms and initiate a new Form to pass to my class.

Many thanks for your time everyone!

Son_Of_Diablo
  • 89
  • 3
  • 12