生成用于创建具有当前值的对象的代码

发布于 2024-12-03 12:32:55 字数 819 浏览 3 评论 0原文

我有这样的场景,我认为这一定很常见:

class Parameter
{
  public int someInt;
  private decimal someDecimal;
  public SubParameter subParameter;
}

class SubParameter
{
  public string someString { get; set; }
}

我在调用以 Parameter 作为参数的方法时有一个断点。我想编写一个单元测试,在其中使用相同的精确值(参数对象“树”的副本)调用此方法。 在这种情况下,编写许多行来声明和初始化类的所有字段和属性是非常乏味的,这些字段和属性本身可能是非原始的等。 如果我可以右键单击参数变量,然后自动生成代码来创建这样的对象,那就太好了。

因此,如果在我的断点处,我的 Parameter 对象具有良好的值

Parameter parameter = new Parameter
  {
    someInt = 42, 
    someDecimal = 42.42m, 
    subParameter = new SubParameter { someString = "42" }
  };

,那么将生成该代码。然后我可以使用生成的代码进行单元测试。

这样的事存在吗?


编辑:

我想我一直不清楚。我非常清楚如何自己手工编写代码。

我想要的是,当我遇到断点并观察复杂变量(或与此相关的任何变量)时,我希望能够说:为我生成创建该变量克隆的代码。我将使用生成的代码进行单元测试。

存在这样的工具吗?

I have this scenario, which I think must be pretty common:

class Parameter
{
  public int someInt;
  private decimal someDecimal;
  public SubParameter subParameter;
}

class SubParameter
{
  public string someString { get; set; }
}

I have a breakpoint at a call to a method that takes a Parameter as a parameter. I want to write a unit test where I call this method with the same exact value (a copy of the Parameter object "tree").
It is very tedious in this case to write the many lines declaring and initializing all the fields and properties of the class, which themselves might be non-primitive etc.
It would be nice if I could just right-click on the parameter variable and then have code auto-generated to create such an object.

So if at my breakpoint, my Parameter object has the value

Parameter parameter = new Parameter
  {
    someInt = 42, 
    someDecimal = 42.42m, 
    subParameter = new SubParameter { someString = "42" }
  };

well, then that code would be generated. I could then use the generated code for my unit test.

Does such a thing exist?


Edit:

I guess I have been unclear. I know perfectly well how to write the code myself by hand.

What I want is that when I am hitting a breakpoint and watching a complex variable (or any variable for that matter), I want to be able to say: Generate code for me that creates a clone of this variable. I would use the generated code for my unit test.

Does such a tool exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日记撕了你也走了 2024-12-10 12:32:55

只需创建一个帮助器方法来为您创建参数:

public void CreateParameter()
{
    return new Parameter
    {
        someInt = 42, 
        someDecimal = 42.42m, 
        subParameter = new SubParameter { someString = "42" }
    };
}

示例使用

[TestMethod]
public void MyTest()
{
    SomeClass.MethodBeingTested(CreateParameter());
}

如果您想要一个特定的参数值,则修改返回的参数或提供允许您提供该值的重载:

[TestMethod]
public void MyTest()
{
    Parameter parameter = CreateParameter();
    parameter.someInt = 23;
    SomeClass.MethodBeingTested(parameter);
}

我通常有我的 CreateParameter 用随机值填充参数,以减少单元测试对于某些值“偶然”通过但对于其他值失败的可能性。

Just create a helper method to create the parameter for you:

public void CreateParameter()
{
    return new Parameter
    {
        someInt = 42, 
        someDecimal = 42.42m, 
        subParameter = new SubParameter { someString = "42" }
    };
}

Sample use

[TestMethod]
public void MyTest()
{
    SomeClass.MethodBeingTested(CreateParameter());
}

If you want to have a specific parameter value then modify the returned parameter or provide an overload which allows you to supply that value:

[TestMethod]
public void MyTest()
{
    Parameter parameter = CreateParameter();
    parameter.someInt = 23;
    SomeClass.MethodBeingTested(parameter);
}

I usually have my CreateParameter populate the parameter with random values to reduce the possibility that the unit test happens to pass "by chance" for certain values, but will fail for others.

猥琐帝 2024-12-10 12:32:55

您可以使用 TestInitialize 来初始化测试方法:

    [TestClass]
    public class UnitTest1
    {
        Parameter _parameter = null;

        [TestInitialize]
        public void Initialize()
        {
            _parameter = new Parameter
            {
                someInt = 42,
                someDecimal = 42.42m,
                subParameter = new SubParameter { someString = "42" }
            };
        }

        [TestCleanup]
        public void Cleanup()
        {
            _parameter = null;
        }

        [TestMethod]
        public void MyTest1()
        {
           // test _parameter
        }

        [TestMethod]
        public void MyTest2()
        {
           // test _parameter
        }
    }

You can use TestInitialize for initialize test methods:

    [TestClass]
    public class UnitTest1
    {
        Parameter _parameter = null;

        [TestInitialize]
        public void Initialize()
        {
            _parameter = new Parameter
            {
                someInt = 42,
                someDecimal = 42.42m,
                subParameter = new SubParameter { someString = "42" }
            };
        }

        [TestCleanup]
        public void Cleanup()
        {
            _parameter = null;
        }

        [TestMethod]
        public void MyTest1()
        {
           // test _parameter
        }

        [TestMethod]
        public void MyTest2()
        {
           // test _parameter
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文