views:

191

answers:

2

I am trying to test that a method to load a UI matrix is being loaded properly. The test fails unless I tell the mock framework to ignore the argument passed, giving me the following message:

Rhino.Mocks.Exceptions.ExpectationViolationException : ITimeSheetMatrixWidget.Load (Smack.ConstructionAdmin.Domain.TransferObjects.TimeSheetDtoAssembler +d__1); Expected #1, Actual #0.

What is interesting is that the message is somehow picking up a call made to another object that assembles DTOs from the domain model - I don't get it!

Here is the interface / method Sut is:

public interface ITimeSheetMatrixWidget : IMatrixWidget {
    .....
    void Load(IEnumerable<DynamicDisplayDto>activities);
    .....

}

And here is the test:

[Test]
public void SettingTheWidget_TriggersLoad_NonProjectActivities() {
        var f = _getFacade();
        // create test activities
        TestDataFactory.SetupTestActivities(f);
        Assert.That(f.NonProjectDtos.Count(), Is.GreaterThan(0));

        // create the presenter
        var filterService = MockRepository.GenerateStub<IProjectFilterService>();
        var view = MockRepository.GenerateStub<ITimeSheetView>();
        var timeSheetPresenter = new TimeSheetPresenter(f, filterService, view);

        // inject the mocked widget & trigger the Load
        var widget = MockRepository.GenerateMock<ITimeSheetMatrixWidget>();
        timeSheetPresenter.ActivityMatrix = widget;

        widget.AssertWasCalled(x => x.Load(f.NonProjectDtos), 
            mo =>mo.IgnoreArguments()); <-- ok, but not useful
        //widget.AssertWasCalled(x => x.Load(f.NonProjectDtos)); <-- generates the Exception
    }

Can someone explain the failure message?

As an aside, I did post this on the Rhino Mocks forum this morning but traffic there looks like it is very low.

Thank you for your help! Berryl

A: 

I think it expected one call but got none.

Bruno Martinez
+1  A: 

Rhino was stating the fact that the way the test was laid out, I wasn't getting the call I'd told it to expect. The test below is an effective way to test an IEnumerable argument:

[Test]
public void ProjectMatrix_Injection_IsLoaded()
    {
        _projectMatrix = MockRepository.GenerateMock<ITimeSheetMatrixWidget>();

        var dtos = _facade.ProjectDtos;
        _projectMatrix.Expect(x => x.Load(Arg<IEnumerable<DynamicDisplayDto>>.List.Equal(dtos))).Return(dtos.Count());

        new MatrixEntryService(_facade, _projectMatrix, _nonProjectMatrix, _totalMatrix);

        _projectMatrix.VerifyAllExpectations();
    }

The first trick is using the Rhino argument constraints:

Arg<IEnumerable<DynamicDisplayDto>>

The second trick is to use the List extension, instead of Is:

List.Equal(dtos)
Berryl