0

Is it possible for visual studio to create a unit test with the parameters that are passed into a method? For example I have a method with this signature:

internal static IEnumerable<Asset> GetAssetsToBuy(Dictionary<string, Asset> assets, string selectedPortfolio, string selectedAllocation)
{
    throw new NotImplementedException();
}

This method was just added to an existing application that collects data from a service, formats it, then calls this method to perform a calculation on it. It would be useful to be able to start developing this new feature by creating a unit test. Is there any way to extract those values to the unit test?

Edit:

I don't think this question is clear as stated. What I'd like to do it get the values at runtime, but copy them to the unit test. For example if I use Newtonsoft.Json and serialize assets then in the unit test add the serialized string as a constant and deserialize it in the test. I'm wondering if there is a way to do this without serializing the values.

Duplicate of Debugger Visualizer to generate Object Initializer code

Adam
  • 1,825
  • 1
  • 17
  • 24
  • A classic way is to create `IAssetData` and method will be `GetAssetsToBuy(IAssetData)`. So, you can provide UT-specific implementation of `IAssetData` to test you method. Often, this is done on class level. – T.S. Jan 23 '19 at 03:06
  • @T.S. can you elaborate on this or link to an article explaining it? I've never heard of that before. But in this case the I just want to extract the data that was passed to the method at runtime and then add it as a constant. This isn't really a question about unit testing now that I think about it. What I want to do is copy assets, then paste and create something like `public static readonly Dictionary assets = new Dictionary { { "key1", new Asset(1, 1, "ticker") } };` – Adam Jan 23 '19 at 03:17
  • 1
    Everything you talking about is already "there". Look for ninject, dependency injection, MOQ. – T.S. Jan 23 '19 at 15:37
  • @T.S. Edited the post to try and make the question clearer. Dependencies are not the issue, it's the values in those dependencies that I want to preserve. – Adam Jan 23 '19 at 17:54
  • So you're trying to export some object from the debugger, and have it automatically generate a C# declaration of an object with the same values? I'm not aware of anything built in that would do that, and asking for a tool to do that would be off-topic, and asking how to write a tool to do that would be too broad. – mason Jan 23 '19 at 18:00
  • @mason Thanks for that wording, it led me to this question which has the wording I was looking for. https://stackoverflow.com/questions/4209082/debugger-visualizer-to-generate-object-initializer-code – Adam Jan 23 '19 at 19:17
  • Possible duplicate of [Debugger Visualizer to generate Object Initializer code](https://stackoverflow.com/questions/4209082/debugger-visualizer-to-generate-object-initializer-code) – Adam Jan 23 '19 at 19:17

0 Answers0