---
title: "Testing loops?"
url: "https://osherove.com/blog/2008/3/9/testing-loops.md"
canonical: "https://osherove.com/blog/2008/3/9/testing-loops.html"
published: "2008-03-09T07:29:49"
updated: "2008-03-09T07:29:49"
tags: ["Agile", "Unit Testing"]
author: "Roy Osherove"
description: "Tommy writes in on my book forum : \" Hi, This is an real world example method where I'm confused how to unit test. ------------------8<------------------- public List<RuleDescription> PrepareCandidates(RuleDescription…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Testing loops?

By Roy Osherove March 9, 2008 In [Agile](https://osherove.com/blog/category/agile), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published March 9, 2008; technical details may be outdated.

Tommy writes in on [my book forum](http://www.manning-sandbox.com/thread.jspa?messageID=68202#68202):

"

Hi,
This is an real world example method where I'm confused how to unit test.
\------------------8<-------------------
public List<RuleDescription> PrepareCandidates(RuleDescription ruleDescription, RuleType ruleType)
{
List<RuleDescriptionType> ruleDescTypeList = \_dataAccessor.GetRuleDescriptionTypeList(ruleType);
List<RuleDescription> ruleDescriptionList = new List<RuleDescription>();
foreach (RuleDescriptionType type in ruleDescTypeList)
{
ruleDescriptionList.AddRange(\_dataAccessor.GetRuleDescriptionList(type, ruleDescription.Extent));
}
return ruleDescriptionList;
}
\------------------8<-------------------
The classes RuleType, RuleDesctiptionType and RuleDescription contains many attributes which some are other complex objects.
The method does not contain any real logic, just adding data to lists from a database (through the \_dataAccessor-object).
Does that mean that I should not unit test this method? Or does it mean that the method is poorly designed? Can or should I mock the \_dataAccessor-object?
Lets say that the method returns 5 elements in the list for given parameters (with say 6 importants attributes in each). That's a lot of data to prepare for a test if I want to write expectations for the return value of the method.
I often stumble in to these situations when I'm unit testing where I become overwhelmed by the amount of data I have to insert. I guess I have the wrong approach, but where do I think wrong?
/Tommy

"

Heres my answer:

I would test this method as it does contain some form of logic (a loop).
There are two ways to test it:

- Integration test: Use the real database (data accessor) which forces you to have this data available somewhere and have everything running.
- unit test: That is what I'd go for here I think.
    here is how you could unit test this:

- Stub out the data accessor to return only 1-3 values (remember to write the test a simply as possible to prove your point), and only send in 1-3 values to the method, and assert that you get back an expected collection.
- What you are testing is: that it loops on all of them, and that is asks for data from the accessor
    , so your tests need to vary on the nuber of objects being passed in, and the data returned from the stub accessor (at least two or three tests I would think).
- when these three pass, you can be certain to a degree that the method does what its supposed to do.

## More in Agile

[All posts in Agile](https://osherove.com/blog/category/agile)

- Apr 14, 2008

    ### [Creating a AutoMockingContainer with Microsoft Unity - Pretty darn simple.](https://osherove.com/blog/2008/4/14/creating-a-automockingcontainer-with-microsoft-unity-pretty.html)

- Mar 15, 2008

    ### [Belgium TechDays Slides and demos, and my travel schedule](https://osherove.com/blog/2008/3/15/belgium-techdays-slides-and-demos-and-my-travel-schedule.html)

- Mar 9, 2008

    ### [Testing ASP.NET MVC Controllers with Typemock Isolator](https://osherove.com/blog/2008/3/9/testing-aspnet-mvc-controllers-with-typemock-isolator.html)

- Mar 1, 2008

    ### [What is a good example of unit test over specification?](https://osherove.com/blog/2008/3/1/what-is-a-good-example-of-unit-test-over-specification.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
