2

I'm learning unit testing. How would the following method be unit tested on VS 2010?

private string[] lstStrCalculatePoints() { string[] lstStrPoints = new string[2]; if (lblKPIAResult.Text.Contains("Exceeds")) { lstStrPoints[0] = "AHT"; } if (lblKPIBResult.Text.Contains("Exceeds")) { lstStrPoints[1] = "QA"; } return lstStrPoints; } 

Please provide example. Thanks!

4
  • Inject a dependency on your class that substitutes lblKPIAResult and assert your logic against it. Where did you get stuck? Commented Apr 11, 2014 at 19:28
  • where is lblKPIAResult defined and assigned? Commented Apr 11, 2014 at 19:29
  • Essentially I want to test this. What is the proper code to test? Am I missing something? Commented Apr 11, 2014 at 19:35
  • Move those boolean tests on those impossibly-named variables out to their own methods. Test those. Mock those away while testing the function proper and verify that the entries you expect to be present are present. Commented Apr 11, 2014 at 20:08

3 Answers 3

5

I'd create two new methods:

public static string GetKpiPoint(string kpiResult, string ifExceeds) { return kpiResult.Contains("Exceeds") ? ifExceeds : null; } public static string[] CalculatePoints(string kpiAResult, string kpiBResult) { return new string[] { GetKpiPoint(kpiAResult, "AHT"), GetKpiPoint(kpiBResult, "QA") }; } 

then call it from your exising one:

private string[] lstStrCalculatePoints() { return CalcualtePoints(lblKPIAResult.Text, lblKPIBResult.Text); } 

EDIT: The ways you can test this method depend on your test framework, but if you're using NUnit you can do something like:

[Test] [TestCase("Exceeds", "Exceeds", new string[] { "AHT", "QA" })] [TestCase("NoMatch", "NoMatch", new string[] { null, null })] [TestCase("Exceeds", "NoMatch", new string[] { "AHT", null })] [TestCase("NoMatch", "Exceeds", new string[] { null, "QA" })] public void CalculatePointsTest(string kpiA, string kpiB, string[] expected) { CollectionAssert.AreEqual(expected, CalculatePoints(kpiA, kpiB)); } 
Sign up to request clarification or add additional context in comments.

2 Comments

You should still show him how to test CalculatePoints. That was the question after all :P
@Lee No need for the [Test] attribute .. [TestCase] is more than sufficient
0

I would not test private methods directly.

You test them indirectly by testing public methods that invoke private methods. This is to prevent you from breaking encapsulation, and it will allow you to refactor your code without having to update your tests unnecessarily.

So long as the public interface works as defined, I do not care what the class does behind the scenes so long as I fully test the public interface's specification.

PS: protected methods are a different story however, but beyond the scope of the question.

Comments

0

From the lblKPI<n>Result attributes I guess you're in a WinForm application, right? You will be better off separating your presentation logic from your view (the Form where lstStrCalculatePoints lives) and put it in a Presenter (or similar). Read up on eg. Model-View-Presenter (MVP) or another MVC deriviate pattern. Do your unit testing on the Presenter class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.