开发者

How to set foreach item expectation in rhino mocks 3.6

开发者 https://www.devze.com 2023-04-12 08:30 出处:网络
I want to be able to do something like this: IProcessDetails detailprocessor = MockRepository.GenerateMock();

I want to be able to do something like this:

IProcessDetails detailprocessor = MockRepository.GenerateMock(); detailprocessor.Expect(p => p.Process(null)) .Repeat.ForEachItemIn(details);

Here is the code I want to test

public class Detail
{

}

public class Master
{
    public Master(IEnumerable<Detail> details)
    {
        Details = details;
    }

    public IEnumerable<Detail> Details { get; private set; }
}

public interface IProcessDetails
{
    void Process(Detail detail);
}

public interface IProcessMasters
{
    void Process(Master master);
}

public class MasterProcessor :
    IProcessMasters
{
    public MasterProcessor(IProcessDetails detailProcessor)
    {
        DetailProcessor = detailProcessor;
    }

    IProcessDetails DetailProcessor;

    #region IProcessMasters Members

    public void Process(Master master)
    {
        foreach (var detail in master.Details)
        {
            DetailProcessor.Process(detail);
        }
    }

    #endregion
}

I want to verify that each detail is processed but only once and I'm not sure how to do it

Here is my current test:

    public void GivenAMasterWithDetailsWhenProcessedThenAllDetailsAreProcessed()
    {
        IEnumerable<Detail> details = new List<Detail>()
        {
            new Detail(),
            new Detail()
        };

        Master master = new Master(
            details);

        IProcessDetails detailprocessor = MockRepository.GenerateMock<IProcessDetails>();
        //what I want or something similar
        //detailprocessor.Expect(p => p.Process(null))
        //    .Repeat.ForEachItemIn(details);

        //what it appears I can do
        detailprocessor.Expect(p => p.Process(null))
            .Constraints(List.OneOf(details))
            .Repeat.Times(details.Count());

        IProcessMasters masterprocessor = new MasterProcessor(
            detailprocessor);

        masterprocessor.Process(master);

     开发者_运维知识库   detailprocessor.VerifyAllExpectations();
    }

SOLVED -- I figured it out:

What I needed was a better understanding of rhino mocks. Using a strict mock and a simple foreach loop solved the problem

here is my test now:

    public void GivenAMasterWithDetailsWhenProcessedThenAllDetailsAreProcessed()
    {
        IEnumerable<Detail> details = new List<Detail>()
    {
        new Detail(),
        new Detail()
    };

        Master master = new Master(
            details);

        IProcessDetails detailprocessor = MockRepository.GenerateStrictMock<IProcessDetails>();

        foreach (var detail in details)
        {
            detailprocessor.Expect(p => p.Process(detail))
                .Repeat.Once();
        }

        IProcessMasters masterprocessor = new MasterProcessor(
            detailprocessor);

        masterprocessor.Process(master);

        detailprocessor.VerifyAllExpectations();
    }
0

精彩评论

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