开发者

Why do I get an ExpectationViolationException when using a Stub?

This test using NUnit 2.5 and Rhino.Mocks 3.5:

[TestFixture]
public class ExcelWorksheetColumnTests
{
        private const string _name = "F1";
        private const int _index = 0;

        private MockRepository _repo;


        [SetUp]
        public void SetUp(){_repo = new MockRepository();}


        private ExcelWorksheetColumn CreateColumnUsingFixtureFieldsButWith(IExcelWorksheet excelWorksheet)
        {
            return new ExcelWorksheetColumn(_name, _index, excelWorksheet);
        }

        [Test]
        public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
        {
            var stubWorksheet = _repo.Stub<IExcelWorksheet>();

            ExcelWorksheetColumn column =
                CreateColumnUsingFixtureFieldsButWith(stubWorksheet);

            stubWorksheet
               .Stub(p => p.GetSelectedShootColumnType(column))
               .Return(ShootColumnType.Generic);

            _repo.ReplayAll();

            bool itHappened = false;

            column.SelectedShootColumnTypeChanged 
                += (s, e) => { itHappened = true; };

            column.SelectedShootColumnType = ShootColumnType.SubjectId;

            Assert.That(itHappened);
        }

        [TearDown]
        public void TearDown()
        {
            _repo.ReplayAll();
            _repo.VerifyAll();
        }
}

Fails with this message:

Rhino.Mocks.Exceptions.ExpectationViolationException : IExcelWorksheet.SetSelectedShootColumnType( ClassPhotolink.Model.CreateSubjectList.ExcelWorksheetColumn, SubjectId);

Expected #0, Actual #1.

I thought Stubs were for returning values or ignoring method calls. Why is it trying to verify a call to

IExcelWorksheet.SetSelectedShootColumnType(
    IExcelWorksheetColumn column, 
    ShootColumnType type)

?

In the rhino mocks wiki it says with regards to the Stub extension method "Remember that by default Rhino Mocks will ignore unexpected methods calls."


Edits of Test.

Removing the SetUp & TearDown code, this still fails:

    [Test]
    public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
    {
        var repo = new MockRepository();
        var stubWorksheet = repo.Stub<IExcelWorksheet>();

        ExcelWorksheetColumn column = CreateColumnUsingFixtureFieldsButWith(stubWorksheet);

        stubWorksheet
            .Stub(p => p.GetSelectedShootColumnType(column))
            .Return(ShootColumnType.Generic);

        repo.ReplayAll();

        bool itHappened = false;

        column.SelectedShootColumnTypeChanged += (s, e) => { itHappened = true; };

        column.SelectedShootColumnType = ShootColumnType.SubjectId;

        Ass开发者_JAVA技巧ert.That(itHappened);
    }

but this works as I hoped.

    [Test]
    public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
    {
        var stubWorksheet = MockRepository.GenerateStub<IExcelWorksheet>();

        ExcelWorksheetColumn column = CreateColumnUsingFixtureFieldsButWith(stubWorksheet);

        stubWorksheet
            .Stub(p => p.GetSelectedShootColumnType(column))
            .Return(ShootColumnType.Generic);

        bool itHappened = false;

        column.SelectedShootColumnTypeChanged += (s, e) => { itHappened = true; };

        column.SelectedShootColumnType = ShootColumnType.SubjectId;

        Assert.That(itHappened);
    }

So it seems to have something to do with repo.Stub() versus MockRepository.GenerateStub();


Using Stub() means that you do expect the method call. Unexpected calls are calls to methods on which no expectation has been set.

update

I'm wondering now if the problem is with your use of setup, teardown, and record/replay, which are unnecessary.

What happens if you write the test this way:

[TestFixture]
public class ExcelWorksheetColumnTests
{
        private const string _name = "F1";
        private const int _index = 0;

        private ExcelWorksheetColumn CreateColumnUsingFixtureFieldsButWith(IExcelWorksheet excelWorksheet)
        {
            return new ExcelWorksheetColumn(_name, _index, excelWorksheet);
        }

        [Test]
        public void When_SelectedShootColumnType_Is_Changed_Raises_SelectedShootColumnTypeChanged_Event()
        {
            var stubWorksheet = MockRepository.Stub<IExcelWorksheet>();

            ExcelWorksheetColumn column =
                CreateColumnUsingFixtureFieldsButWith(stubWorksheet);

            stubWorksheet
               .Stub(p => p.GetSelectedShootColumnType(column))
               .Return(ShootColumnType.Generic);

            bool itHappened = false;

            column.SelectedShootColumnTypeChanged 
                += (s, e) => { itHappened = true; };

            column.SelectedShootColumnType = ShootColumnType.SubjectId;

            Assert.That(itHappened);
        }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜