views:

176

answers:

3

My class has like 30-40 properties, and I really want to unit test.

But I have to create a moq instance (many of them, with different combinations etc).

Is there an easy way? This is real work!

My class can't be refactored, "trust me" (hehe, no really it can't, they are just properties of the object that are very tightly coupled).

+14  A: 

Sounds like you need to do some major refactoring. I would start by taking a good look at the single responsibility principle, and making classes that will only have 1 reason to change. Once you break out functionality into separate classes that deal with only 1 responsibility, you can start writing tests for those classes, and they shouldn't take a page-full of mock objects.

This is the advantage of test-driven development -- you immediately run into the problems caused by huge classes, and are driven to avoid them if you want to be able to write tests.

Kaleb Brasee
Although I agree to your point, .NET's System.Web.UI.Control class has exactly 30 properties.
Ciwee
+1 for the single responsibility principle.
Ben S
@André Pena: I also think that it's in need of some major refactoring.
Ben S
Another +1 for the SRP. The fact that you talk about 30 properties also suggests you're doing state-based testing, rather than thinking about interactions and behaviors.
kyoryu
+4  A: 

Personally, I don't think you need to try every combination to test your class.

You mention lots about properties, but little about behavior. Shouldn't the tests be about behavior more than state?

duffymo
+3  A: 

There could well be situations where, due to the nature of the class, there are a lot of legitimate properties. I know, I've been there and done that. When examining that class, it is important to determine that each property really does belong in the one class, and not elsewhere. Single Responsibility Principle comes in play here.

Unfortunately, to break any tight coupling, it will take some time and effort to refactor. Just suck it up and get 'er done!

Grant Palin