开发者

Rhino Mocks: Stubbing an IEnumerator<T>

开发者 https://www.devze.com 2023-03-27 05:25 出处:网络
I\'m developing a little .net 2.0 project. I get to the point where I need to test some class\'s method which takes an IEnumerator. As a few days ago I learnt to use Rhino Mocks I wrote the following

I'm developing a little .net 2.0 project. I get to the point where I need to test some class's method which takes an IEnumerator. As a few days ago I learnt to use Rhino Mocks I wrote the following test

[Test]
public void GetEnumerator_ValidList_ReverseIteration()
{
    MockRepository mMocks = new MockRepository();
    IEnumerator<string> mStubEnumerator = mMocks.Stub<IEnumerator<string>>();
    IProcessCommandFactory mStubFactory = mMocks.Stub<IProcessCommandFactory>();

    using (mMocks.Record())
    {

        mStubEnumerator.MoveNext();
        LastCall.Return(true);

        Expect.Call(mStubEnumerator.Current);
        LastCall.Return("Organization");

        mStubEnumerator.MoveNext();
        LastCall.Return(true);

        Expect.Call(mStubEnumerator.Current);
        LastCall.Return("Algorithm");

        mStubEnumerator.MoveNext();
        LastCall.Return(true);

        Expect.Call(mStubEnumerator.Current);
        LastCall.Return("ProcessTemplate");

        mStubEnumerator.MoveNext();
        LastCall.Return(false);

    }

    DeleteStrategy mStrategy= new DeleteStrategy(   mStubFactory,
                                                        "S1",
                                                        true);
    mStrategy.Load(mStubEnumerator);

    ... meaningless code...

}

this is the method code

public void Load(IEnumerator<string> pProcessCommmandNames)
{
    while (pProcessCommmandNames.MoveNext())
开发者_高级运维    {
        string bCommandName= pProcessCommmandNames.Current;

        ... doing something with the string...
    }
}

When I try to run the, it won't stop since it uses only the first MoveNext() injected value and starts a loop.

I would appreciated if someone could poit me out what I'm doing wrong?

Thanks


From what I am seeing you are passing Load() the stub mStubEnumerator. With out setting any of the properties on the stub, all of the methods are going to do nothing and all of the properties are going to return null.

Here is a great article by Martin Fowler reviewing the differences between Mocks, Stubs, and Fakes.

So to make this test work, I would consider using a fake:

MockRepository mMocks = new MockRepository();
IEnumerator<string> mFakeEnumerator = new List<string>();
mFakeEnumerator.Add("Organization");
mFakeEnumerator.Add("Algorithm");
mFakeEnumerator.Add("ProcessTemplate");
IProcessCommandFactory mStubFactory = mMocks.Stub<IProcessCommandFactory>();
...

Now I am guessing that the test will pass, or it will be closer to passing.

Without seeing more of the code, however, I cannot be sure that it would be effectively testing what you set out to achieve.


If you just need an IEnumerator<String>, there's no need to create a mock. Simply create a List<String> and pass in it's enumerator:

var commandNames = new List<String> {"one", "two", "three"};
DoSomething(commandNames.GetEnumerator());
0

精彩评论

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

关注公众号