Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking large data objects

I am writing unit tests for our C# project. In a couple of the classes I'm testing, we have methods that look like this:

public ClassWithLotsOfProperties doStuffINeedToTest(AnotherClassWithLotsOfProperties input)
{
    //do stuff
}

I'm planning to test them by mocking a couple AnotherClassWithLotsOfProperties objects, making sure I know what ClassWithLotsOfProperties objects to expect when the first objects are passed in, and then verifying that I get the results I'm looking for. Right now, our team is confident that the classes being tested are working properly, so I'm running the program, getting it to a state where doStuffINeedToTest() is called, and setting a breakpoint at the end of the method so that I can write down what's inside the state of the method's input and output objects.

The downside to this method is that AnotherClassWithLotsOfProperties and ClassWithLotsOfProperties both have dozens of properties, meaning that writing down their values from the debugger and then setting up their mocks in the unit test is going to be tedious. Is there a faster way to set up these unit tests? Right now, I'm using NUnit and Moq. Our team wants to stick with NUnit, but I'm willing to look at another mocking framework.

In particular, one thing that I think would be nice is if I can have Visual Studio or the mocking framework automatically generate mock objects for me when I run the program to set up the method calls I want to base my tests on. Is there any way to do anything like this?

like image 376
Kevin Avatar asked Jan 16 '13 02:01

Kevin


1 Answers

Use AutoFixture. It will make it very easy to generate test data. Here's an example:

var testData = fixture.CreateAnonymous<ClassWithLotsOfProperties>();

Basically it will iterate all the properties of the class and set each one with a value for that type.

like image 97
Ufuk Hacıoğulları Avatar answered Oct 17 '22 07:10

Ufuk Hacıoğulları