开发者

Generate code for creating an object with current values

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 b开发者_如何学JAVAe 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?


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.


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
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜