How to test a method on a class that calls another method in the same class using Rhino Mocks

I have a class that solves an equation using an approximation, evaluates the approximation, and then refines the approximation (halving method), rinsing and repeating until an answer appears. To do this, it needs to go and get different values ​​from other complex classes. You also need to call the method internally again to determine how to change the assumption before running the method again. I was able to check the calculation method:

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

      

However, now I want to test the new hypothesis approximation method that calls the calculation method:

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

      

This is a test, but its clearly not working. I know I am missing something (maybe something fundamentally important in Rhino 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);

  }

      

+2


a source to share


1 answer


Usually when writing a test it turns out to be very difficult, which means that you have to split the class into smaller classes. In your case, I am assuming that your calculation method should be in one class and your approximate method (the one that uses the calculation method) should be in another class. The second class would then use the first class. In your tests, you can mock the test class so that you can easily control what the calculation method returns.



0


a source







All Articles