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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With