I have an interface and a
method. I want to know whether i can test my method without an
implementation of the interface. Any ideas? Are there any usefull
things when it comes to unit testing for this kind of stuff?
C#
publicinterfaceICalcStrategy
{
doubleCalculate(double amount);
}
publicstaticclassNumberCalculation
{
publicstaticdouble[] CalculateTaxes(double[] numbers, ICalcStrategy calcStrategy)
{
double[] result = newdouble[numbers.Length];
for (int i = 0; i < numbers.Length; i++)
result[i] = calcStrategy.Calculate(numbers[i]);
return result;
}
}
It would be great if someone could help me with this!
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sam HobbsRank #107 · 20.5K reputationPosted Nov 2, 2009, 8:19 AM EST
0
I assume you are asking so you can make testing more convenient. Do you mind an alternative? What I would do is to create a test driver. I would create the testing project within the same solution as the object to be tested. Solutions can have multiple projects. The most convenient way to create a project in an existing solution is to right-click on the solution in the solution explorer and choose "Add | New Project". Then I would set the test program as the Startup Project. I also use "Build | Configuration Manager" to determine what projects to build. That last part does not matter much unless I am modifying more than one of the solution's projects at a time and therefore multiple projects get compile errors. If I set the configuration so it builds only the one I am working on, then I don't have to deal with errors from a project I am currently not working on.
Does that help? I really think that doing it that way, it is as easy or easier than doing it the way you are thinking to do it.
Thanx for the answer but unfortunately it didnt help me. This is a question from a test im trying to solve. Fist i thought this was a question connected to programming. But i rather think that the answer is that you can use testing framworks like NUnit. In that case the framwork helps you to test methods without having the whole implementation of e.g. interfaces. Am i understandable? :)
0
Sam HobbsRank #107 · 20.5K reputationPosted Nov 2, 2009, 11:10 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Nov 2, 2009, 8:19 AM EST
David5 reputationPosted Nov 2, 2009, 8:34 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Nov 2, 2009, 11:10 AM EST
David5 reputationPosted Nov 2, 2009, 11:14 AM EST