RandomEngy / UnitTestBoilerplateGenerator

An extension for Visual Studio that generates a unit test boilerplate from a given class, setting up mocks for all dependencies. Supports NUnit, Visual Studio Test, XUnit and many mock frameworks.
https://marketplace.visualstudio.com/items?itemName=RandomEngy.UnitTestBoilerplateGenerator
MIT License
64 stars 17 forks source link

Primary constructors #36

Closed tomasKosar closed 11 months ago

tomasKosar commented 11 months ago

Installed product versions

Description

Fields declarations and fields initialisation does not work when primary constructor is used instead of traditional constructor.

Steps to recreate

  1. Use $MockFieldDeclarations$ and $MockFieldInitializations$ in template to generate fields declarations and fields initialisation
  2. Create a class with only primary constructor where few dependencies are injected
  3. Generate unit tests for this class
  4. Injected dependencies from primary constructor are not auto generated

Current behavior

Fields declarations and fields initialisation are not created when primary constructor is used.

Expected behavior

Fields declarations and fields initialisation are created when primary constructor is used.

Notes

If I convert class from primary constructor to traditional constructor then everything works as expected. This could very well be because this is relatively new C# feature and obviously this extension could not anticipate this. https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#primary-constructors I very much like this extension and I appreciate all the work on this. If anyone can point me to how to solve this or implement this functionality then it would be appreciated. I understand that with Copilot there are other ways how to quickly generate unit tests boiler plate but I still do see this extension as a big help.

RandomEngy commented 11 months ago

https://github.com/RandomEngy/UnitTestBoilerplateGenerator/blob/master/src/Services/TestGenerationService.cs CollectTestGenerationContextAsync is what you want to modify here. That's the code that examines the class and finds what types it needs as constructor arguments, and populates ConstructorTypes on the TestGenerationContext. It uses Roslyn APIs to check the syntax tree.

The syntax tree visualizer is a good tool to figure out what to look for when examining the class: https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/syntax-visualizer?tabs=csharp

One caveat here is that to provide compatibility with VS 2019, we reference the v1 Microsoft.CodeAnalysis NuGet packages, which means you don't have the actual enum values like SyntaxKind.RecordDeclaration. You have to use the raw numbers that correspond to those syntax types.

To develop you need to install the VS Extension development capability, then you can just F5 and open Sandbox.sln to test.

tomasKosar commented 11 months ago

OK Thanks for pointing me to the right direction.

I think I figured what changes we need to make to implement this. I have them locally and it seems to be working. Its just that I do not have write access for this repo so I am not allowed to push a branch with those changes. I am happy to share them here or push the branch if I would get access.

RandomEngy commented 11 months ago

You can create a pull request to submit your changes.

Basically: fork this repository, make a new branch with your changes on it, then visit the repository on GitHub and it will prompt you to start a pull request.

tomasKosar commented 11 months ago

PR can be found here #37

RandomEngy commented 11 months ago

Resolved by PR #37