UnitTestBoilerplateGenerator icon indicating copy to clipboard operation
UnitTestBoilerplateGenerator copied to clipboard

Feature request: support static classes / methods

Open 304NotModified opened this issue 4 years ago • 3 comments

Installed product versions

  • Visual Studio: 2019 community
  • This extension: 2.5.7

Description

It would be great if static classes / methods are supported

Steps to recreate

Create tests for this class:

    public static class StaticClass
    {
        public static string MyMethod() => "test";
    }

Current behavior

Current code, the code won't compile because of new StaticClass

    [TestClass]
    public class StaticClassTests
    {
        [TestMethod]
        public void MyMethod_StateUnderTest_ExpectedBehavior()
        {
            // Arrange
            var staticClass = new StaticClass();

            // Act
            var result = staticClass.MyMethod();

            // Assert
            Assert.Fail();
        }
    }

Expected behavior

for static methods, no need for new StaticClass


    [TestClass]
    public class StaticClassTests
    {
        [TestMethod]
        public void MyMethod_StateUnderTest_ExpectedBehavior()
        {
            // Arrange
            
            // Act
            var result = StaticClass.MyMethod();

            // Assert
            Assert.Fail();
        }
    }

304NotModified avatar Sep 25 '21 20:09 304NotModified

FWIW, I implemented support for this in my extension. It's a bit different to the boilerplate generator, in that it's nowhere near as customisable but supports a bunch of extra stuff. Just thought it was worth mentioning, as maybe someone who wants to implement it for this extension can get inspired by the code I wrote 👍

mattwhitfield avatar Feb 09 '22 20:02 mattwhitfield

still after 3.5 years this is not yet fixed / implemented...

ValeriuZabulica-Exprs avatar Feb 27 '24 09:02 ValeriuZabulica-Exprs

I actually haven't used this myself since 2017, which is nearly the entire life of the project. Since nobody pays me, I work on features that inspire me, and mocking static classes doesn't really do that. They are hard to test, which is why I use DI and created a tool to help set up mocks for constructor DI classes.

But I understand other people program in different ways and may find other features useful. I have taken a fair number of pull requests that have added nice features and would happily work with you to integrate your own PR, and even give you some code pointers to get started.

Listening to people try to shame me for not implementing their favored feature does not inspire me to do the work.

RandomEngy avatar Feb 28 '24 03:02 RandomEngy