English 中文(简体)
我如何在使用Rhino Mocks的同一类别中使用另一种方法的类别上测试一种方法。
原标题:How do I 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(bisection method), rinse and repeat until the answer appears. In order to do that it needs to go and get various values from other complex classes. It also need to repeatedly call a method within itself to work out how to change the guess before running through the method again. I ve managed to test the calculate 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;
  }

但 我现在要测试新的近似法,即计算法:

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

这是检验,但显然没有奏效。 我知道我没有的东西(可能是对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);

  }
最佳回答

通常,在撰写考试证明非常困难时,这意味着你应分成小班级。 就你而言,我的第一gues是,你的计算方法应属于一个类别,而你的近似方法(使用计算方法的方法)应属于另一个类别。 第二类将使用第一类。 在你的测试中,你可以模拟试验舱,以便你能够轻易控制计算方法的回报。

问题回答

暂无回答




相关问题
How to unit test file permissions in NUnit?

I m trying to unit test file read operations. In this scenario I also need make sure that, if a particular user don t have read access he should get an exception... But somehow I m unable to get it ...

Rhino mocks - does this test look sensible?

I have just crafted the following test using Rhino mocks. Does my test look valid and make sense to those more experienced with mocking? I am a a little confused that I haven t had to use the ...

How to mock Add method of subsonic s SimpleRepository

I m trying to mock the Add method of subsonic SimpleRepository with Rihino mocks, I m using the IRepository Interface but I m new to mocking and dont know how to go from there, can this be done? ...

StrucutureMap RhinoMock Record/Playback, Example needed

I m looking for some examples on how to do the following Mock Tests using StructureMap or Unity with NUnit. I have the following code structure public interface IDAL { List<Model> Method1(...

How do I Fake UpdateModel for ASP.Net MVC?

I am trying to unit test a controller action that uses UpdateModel but I am not correctly mocking the HttpContext. I keep getting the following exception: System.InvalidOperationException: ...

热门标签