13

I am wondering if anything like this exists:

When debugging in visual studio it would be really good if it had the ability to copy an object(all its properties and values) into memory. I am thinking of something that you could put a break point in code -> right click the object -> Click "create moq into clipboard"

Then you could go to your unit test, paste the text which would paste the code to create that object with all of those properties.

I imagine this would save loads of time, and would be really useful for fixing bugs.

One of the difficult things I find with unit test is the manual process in creating the mock objects.

user1619480
  • 533
  • 8
  • 18
  • I think this is a duplicate of https://stackoverflow.com/questions/2927991/copy-object-values-in-visual-studio-debug-mode – Hemaolle Jan 27 '22 at 12:59

2 Answers2

10

I'm a bit late to the party but I created a Visual Studio extension that does something very similar to what you're looking for. It will generate C# code to initialize an object from your visual studio debugging windows.

Here's the extension: Visual Studio Marketplace > Object Explorer

And here's an accompanying blog post: Exporting Objects from the Visual Studio Debugger

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Omar Elabd
  • 1,914
  • 1
  • 18
  • 23
-1

If you find it onerous to create mock objects, there is likely a problem with your design or the way your using mock objects. You shouldn't create mocks for simple 'value' objects - just use the actual objects in your unit tests. For more complex objects that your object-under-test interacts with, the interactions should be few, otherwise you may have excessive coupling between objects. As a rule of thumb, if it's hard to test, the design is probably wrong.

Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33
  • 2
    I think this statement is true for a green fields project. However I am working in an existing system that does not have much in the way of unit tests, and I dont have much time to refactor at present. Problem solving areas within the system means stepping through a lot of stuff which is time consuming. I would be thinking of using this as a debug tool, which could save time and help the transition into a system with more unit tests. – user1619480 Mar 28 '14 at 09:59
  • OK, working with legacy code is a different situation! Short answer: I'm not aware of anything to generate mock code from within the debugger. Long answer: Michael Feathers' Working Effectively With Legacy Code has good advice on other ways to deal with testing legacy code. http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052 – Mike Stockdale Mar 28 '14 at 14:07