Let's say I have a class which has a method to calculate something (it doesn't really matter what). It could look something like this:
class Example
{
public int CalculateStuff(int inputValue)
{
// ...some logic here
}
}
But the inputValue
is always taken from the class itself. So it instead it could look like this:
class Example
{
public int InputValue { get; set; }
public int CalculateStuff()
{
// ...calculate something from 'InputValue'
}
}
Now this is great, but I would like to add a unit test to CalculateStuff()
, so here is my question:
What is the best way to make a clear test? I could simple set the value on the class in the test, like this:
void UnitTest()
{
var example = new Example();
example.InputValue = 42;
Assert.AreEqual(84, example.CalculateStuff());
}
However, I could also make CalculateStuff()
a static
method, with a non-static overload, and then test the static part. The advantage of this (as far as can tell) is that it would also work in the scenario where the InputValue
is something like DateTime.Now
:
class Example
{
public int InputValue { get; set; }
public int CalculateStuff()
{
return CalculateStuff(InputValue);
}
public static int CalculateStuff(int inputValue)
{
// ...calculate something from 'inputValue'
}
}
void UnitTest()
{
Assert.AreEqual(84, Example.CalculateStuff(42));
}
Is there a widely accepted way of doing this? And if so, which? Or does it depend on the specific case?
Aucun commentaire:
Enregistrer un commentaire