J'ai une classe qui résout une équation en utilisant une approximation, évalue l'approximation, puis affine l'approximation (méthode de bissection), rincer et répétez jusqu'à ce que la réponse apparaisse. Pour ce faire, il doit aller chercher diverses valeurs d'autres classes complexes. Il doit également appeler à plusieurs reprises une méthode en lui-même pour déterminer comment changer l'estimation avant de réexécuter la méthode. J'ai réussi à tester la méthode de calcul:Comment tester une méthode sur une classe qui appelle une autre méthode dans la même classe à l'aide de Rhino Mocks
protected double GRPY(double royGuess, ReductionOnYield redOnYield)
{
log.LogEnter();
double d1 = 0D;
double d2 = 0D;
double growth = 0D;
double regularPremiumInMonthm = 0D;
double termSurrenderValue = Convert.ToDouble(illus.GetCashInValue(redOnYield.Month) * GetFundStreamSplit(redOnYield, redOnYield.Month));
for (int i = 1; i <= redOnYield.Month; i++)
{
regularPremiumInMonthm = Convert.ToDouble(illus.RegularPremium.PremiumAmount * Convert.ToDecimal(GetFundStreamSplit(redOnYield, i)));
d1 = (1 + royGuess);
d2 = (redOnYield.Month - (i - 1))/12D;
growth = growth + Convert.ToDouble(regularPremiumInMonthm) * Math.Pow(d1, d2);
}
double gRoy = ((termSurrenderValue - growth))/termSurrenderValue;
log.LogExit();
return gRoy;
}
Cependant, je veux maintenant tester la conjecture nouvelle méthode d'approximation qui appelle la méthode calculate:
protected double SetNewMidPoint(double midPoint, double gStartLow, double gStartHigh, double gMidPoint, ReductionOnYield redOnYield)
{
log.LogEnter();
if ((gStartLow * gStartHigh) > 0)
{
startLow = 0.001D;
startHigh = 0.07D;
midPoint = (startHigh - startLow)/2 + startLow;
gStartLow = GRPY(startLow, redOnYield);
gStartHigh = GRPY(startHigh, redOnYield);
gMidPoint = GRPY(midPoint, redOnYield);
if((gStartLow > 0) && (gStartHigh > 0))
{
midPoint = 0.07D;
}
if ((gStartLow < 0) && (gStartHigh < 0))
{
midPoint = 0D;
}
}
if((gStartLow * gMidPoint) < 0)
{
startHigh = midPoint;
midPoint = (startHigh - startLow)/2 + startLow;
}
if((gStartLow * gMidPoint) > 0)
{
startLow = midPoint;
midPoint = (startHigh - startLow)/2 + startLow;
}
log.LogExit();
return midPoint;
}
C'est le test mais son évidemment ne fonctionne pas . Je sais que je manque quelque chose (peut-être quelque chose à propos de Rhino massivement fondamental Mocks)
[TestMethod()]
public void SetNewMidPointGStartLowTimesGStartHighGreaterThanZeroTest()
{
var quote = MockRepository.GenerateStub<EQuote>();
double growthRate = 0.07;
quote.Request = new Request();
var illustration = MockRepository.GenerateStub<Illustration>(quote, growthRate);
var target = MockRepository.GenerateMock<RegularPremiumReductionOnYieldCalculator_Accessor>(illustration);
double gStartLow = 0.1F;
double gStartHigh = 0.1F;
double gMidPoint = 0.1F;
double startLow = 0F;
double startHigh = 0F;
double midPoint = 0F;
var redOnYield = MockRepository.GenerateStub<ReductionOnYield>(1);
target.Stub(x => x.GRPY(0.001D, redOnYield)).Return(0.07D).Repeat.Once();
target.Stub(x => x.GRPY(0.07D, redOnYield)).Return(0.07D).Repeat.Once();
target.Stub(x => x.GRPY(midPoint, redOnYield)).Return(0).Repeat.Any();
double actual = target.SetNewMidPoint(midPoint, gStartLow, gStartHigh, gMidPoint, redOnYield);
double expected = 0.07D;
Assert.AreEqual(expected, actual);
}