0

I am new to unit testing and am using Visual Studio and I have a public variable in the form called mm_x which is calculated in a function in the form (not in a class) and when I try to do a test I can't find a way to reference it from the UnitTest Method. It was easy to test other variables that are on classes but this particular one I can't find the best way to do it.

Should I:

Put all of the logic for the variable calculation in the form in a business class and then unit test the business class? Keep the form logic in the form and somehow unit test against the form?

As a reference, here's the method IN the form which contains the variable I'm trying to test: (poi_navegacion_individual and MR are 2 classes I use but I'm interested in teting the results of mm_X)

 private void Leer_NDI_tracker(object sender, EventArgs e)
        {

            if (MR != null)
            {
                mm_X = poi_navegacion_individual.POR_x * Convert.ToDecimal(MR.MR0) + poi_navegacion_individual.POR_y * Convert.ToDecimal(MR.MR1) + poi_navegacion_individual.POR_z * Convert.ToDecimal(MR.MR2) + Convert.ToDecimal(MR.MR9);
            }
         }

Thank you,

Matias
  • 539
  • 5
  • 28
  • 4
    The best approach is to put all the calculation logic in a separate class and test just the calculation class. It's never ideal to instantiate a UI component (like a WinForm) inside a unit test. – Silas Reinagel Jan 12 '16 at 21:08

2 Answers2

1

As you mentioned in your question - you should keep all business logic separated from UI elements. This would not only make it easy to test your calculation, but would also make the method reusable by other parts of the program.

Erresen
  • 1,923
  • 1
  • 22
  • 41
1

I'd advise looking into the MVC (Model, View, Controller) method of implementing a Windows Forms app. It helps keep the UI and code separated.

I personally haven't used it since I've only worked with WPF apps where MVVM (Model, View, ViewModel) is used but the reasoning behind the two models is similar in that they aim to separate code and UI.

Take a look here for more information on implementing MVC: How would you implement MVC in a Windows Forms application?

Community
  • 1
  • 1
LWood
  • 417
  • 4
  • 16