开发者

How do I mock this value using Rhino Mocks

Here is the method I'm trying 开发者_Go百科to test:

public override void CalculateReductionOnYield()
    {
        log.LogEnter();
        if (illus.RpFundStreams.Count <= 0)
        {
            throw new InvalidDataException("No regular premium fund streams which are required in order to calculate reduction on yield");
        }
        // Add the individual ReductionOnYield classes to the collection.)
        foreach (RegularPremiumFundStream fs in illus.RpFundStreams)
        {
            foreach (int i in ReductionOnYieldMonths)
            {
                ReductionOnYield roy = new ReductionOnYield(i);
                roy.FundStream = fs;
                ReductionsOnYield.Add(roy);
            }
            foreach (ReductionOnYield redOnYield in ReductionsOnYield)
            {
                if (redOnYield.Month == 0 || illus.RegularPremiumInPlanCurrency == 0M)
                {
                    redOnYield.Reduction = 0M;
                }
                else
                {
                    double[] regPremiums = new double[redOnYield.Month + 1];
                    for (int i = 1; i <= redOnYield.Month; i++)
                    {
                        regPremiums[i - 1] = Convert.ToDouble(-1*redOnYield.FundStream.FundStreamMonths[i].ValRegularPremium);
                    }
                    regPremiums[redOnYield.Month] = Convert.ToDouble(redOnYield.FundStream.GetFundStreamValue(redOnYield.Month));
                    redOnYield.Reduction = Convert.ToDecimal(Math.Pow((1 + Financial.IRR(ref regPremiums, 0.001D)), 12) - 1);
                }
            }
        }

How do I mock all the required classes to test the value of redOnYield.Reduction to make sure that it working properly?

e.g. how do I mock redOnYield.FundStream.GetFundStreamValue(redOnYield.Month) and redOnYield.FundStream.FundStreamMonths[i].ValRegularPremium ?

Is this a valid test? Or am I going about this the wrong way?


without more info on your objects its hard to say, but you want something like:

var fundStream = MockRepository.GenerateStub<TFundStream>();
fundStream.Stub(f => f.GetFundStreamValue(60)).Return(220000M);

var redOnYeild = MockRepository.GenerateStub<TRedOnYeild>();
redOnYeild.Stub(r => r.FundStream).Return(fundStream);


redOnYield is an object returned from iterating ReductionsOnYield. I don't see where this is coming from. If we assume it's a virtual property, then you'll want to create a collection of mock ReductionOnYield objects and stub out ReductionsOnYield to return your mocked collection (or, to make it easier to test, have CalculateReductionOnYield accept an IEnumerable and operate on that collection).

Once you get the ReductionsOnYield issue resolved, Andrew's response of stubbing out the properties will get you where you want to be. Of course, this assumes that FundStream is virtual (so it can be mocked/stubbed) as well as RegularPremiumFundStream's GetFundStreamValue and FundStreamMonths.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜