#28 ArgumentOutOfRangeException when verifying expectations

open
nobody
nmock2 (11)
5
2008-03-12
2008-03-12
SharpMao
No

Description :

When using Mockery.Ordered, but setting no expectation in the using part, the VerifyAllExpectationsHaveBeenMet causes an exception :
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.

Cause :

OrderedExpectations.CurrentExpectation is called without testing there is at least an expectation.

Code to reproduce it :

[TestFixture]
public class Tests
{
private ITest _testMock;
private Mockery _mockery;
[Test]
public void TestEmptyOrdered()
{
_mockery = new Mockery();
_testMock = _mockery.NewMock<ITest>();
using (_mockery.Ordered)
{}
_mockery.VerifyAllExpectationsHaveBeenMet();
}
}

public interface ITest
{
}

Proposed solution :

In OrderedExpectations, replace the HasBeenMet property by the following code :

public bool HasBeenMet
{
get { return expectations.Count==0 || (CurrentExpectation.HasBeenMet && NextExpectationHasBeenMet()); }
}

Discussion

  • Logged In: YES
    user_id=1140914
    Originator: NO

    Hi there

    I've added the fix to the NMock2 project. (see http://nmock2.sourceforge.net\). It will be included in the next release or just use the source code.

    Happy mocking
    Thomas