Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate mocked repository objects quickly using live data? Moq

I'm not even sure if there is a utility that allows this, but it seems like such a common need?

Is there a program/library/component that will generate mocked .NET objects (repositories) that will yield some mocked data as certain database calls.

Allow me to elaborate: If I'm testing a repository (not unit testing, simply testing in a console window) and when I pass a selection criteria to my repository, it returns a list of objects. I would /love/ for an ability to capture that list of objects and convert it to static setup C# code that would serve as a setup for a unit test. My objects are quiet complex and it takes a long time to set them up properly.

Is this possible? My team is using Moq & Ninject

like image 232
Igorek Avatar asked Nov 05 '22 08:11

Igorek


2 Answers

It can be a pain to write them by hand but you only need to get it right once. Then for each successive test you can modify the inputs. I don't know of anyway to save or capture your objects at runtime. I found a webpage that talks about automocking it MAY do what you are after. http://code.google.com/p/moq-contrib/wiki/Automocking

Cheers, Andrew

like image 130
Andrew Avatar answered Nov 15 '22 11:11

Andrew


There are a couple of good ways to deal with this need.

First, you could re-examine why you want to do this. If you need to have a complex object fully configured in order to run your unit test, it could be you're trying to test too much. Complex setup is a test smell, and might (or might not) indicate that you should make a design change.

Sometimes it isn't feasible to make that kind of change, and sometimes that test smell does not really indicate a design change. In those cases, your best bet is serialize the objects (when you say "convert it to static setup C# code", that could be restated as "serialize it as C# instructions for recreating it"). There are a lot of relatively simple ways to serialize to xml, I recommend DataContract or XmlSerialization, they are both very easy to implement and use.

like image 43
tallseth Avatar answered Nov 15 '22 11:11

tallseth