I am refactoring a core method that has 30-odd arguments such that it will have only two.
Currently, unit tests look something like this:
<code>[Test]
public async Task TestA1()
{
BigTestMethod(
paramObj: new PObj {
attrib1 = 'a',
attrib2 = 'b',
//... etc.
attribN = 1
},
param1: 'something',
param2: 'something else',
paramObj2: new QObj {
qA = 1,
qB = 2
};
}
</code>
<code>[Test]
public async Task TestA1()
{
BigTestMethod(
paramObj: new PObj {
attrib1 = 'a',
attrib2 = 'b',
//... etc.
attribN = 1
},
param1: 'something',
param2: 'something else',
paramObj2: new QObj {
qA = 1,
qB = 2
};
}
</code>
[Test]
public async Task TestA1()
{
BigTestMethod(
paramObj: new PObj {
attrib1 = 'a',
attrib2 = 'b',
//... etc.
attribN = 1
},
param1: 'something',
param2: 'something else',
paramObj2: new QObj {
qA = 1,
qB = 2
};
}
I created a new function with 2 class parameters, and I use a TestCaseSource to set certain key values, like the below. [Variable names were changed.]
<code>[TestCaseSource(typeof(TestSourceData), nameof(TestSourceData.SetupTestSources),
[_pValue, null, _value, null, _money])]
public async Task TestA1(Info input, Info expected)
{
input.attr1 = 1;
input.attr2 = 2;
// etc. for other attributes that are not the default values.
expected.attr1 = 1;
// etc.
TestMethod_New(input, expected);
}
// ----------------
public static class TestSourceData
{
private static Info _input;
private static Info _expected;
public public static IEnumerable SetupTestSources (int p1, int p2, int p3, int p4, bool p5)
{
// some preparatory code
//...
_input = new {
param1 = p1,
param2 = p2,
// etc.
}
// same for _expected
yield return new object[] { _input, _expected };
}
</code>
<code>[TestCaseSource(typeof(TestSourceData), nameof(TestSourceData.SetupTestSources),
[_pValue, null, _value, null, _money])]
public async Task TestA1(Info input, Info expected)
{
input.attr1 = 1;
input.attr2 = 2;
// etc. for other attributes that are not the default values.
expected.attr1 = 1;
// etc.
TestMethod_New(input, expected);
}
// ----------------
public static class TestSourceData
{
private static Info _input;
private static Info _expected;
public public static IEnumerable SetupTestSources (int p1, int p2, int p3, int p4, bool p5)
{
// some preparatory code
//...
_input = new {
param1 = p1,
param2 = p2,
// etc.
}
// same for _expected
yield return new object[] { _input, _expected };
}
</code>
[TestCaseSource(typeof(TestSourceData), nameof(TestSourceData.SetupTestSources),
[_pValue, null, _value, null, _money])]
public async Task TestA1(Info input, Info expected)
{
input.attr1 = 1;
input.attr2 = 2;
// etc. for other attributes that are not the default values.
expected.attr1 = 1;
// etc.
TestMethod_New(input, expected);
}
// ----------------
public static class TestSourceData
{
private static Info _input;
private static Info _expected;
public public static IEnumerable SetupTestSources (int p1, int p2, int p3, int p4, bool p5)
{
// some preparatory code
//...
_input = new {
param1 = p1,
param2 = p2,
// etc.
}
// same for _expected
yield return new object[] { _input, _expected };
}
Is this a sound approach?
Would you recommend something different?
New contributor
Guy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2