views:

332

answers:

1

Hi,

Assuming an IMouvement Object which contains some subobjects like ITache, IStockPalette.

public interface IMouvement : IObjectBase<Guid>
{
        ITache Tache { get; set; }
        IStockPalette StockPalOrigine { get; set; }
}

How can I Mock this using Rhino Mocks ?

Assuming this test, what's wrong with this ?

[TestMethod]
public void Can_Validate_EmplacementTransitoireRule()
{
    var mocks = new MockRepository();
    var mouvement = mocks.StrictMock<IMouvement>();
    var manager = mocks.StrictMock<ValidationManager>();

    mouvement.Tache = mocks.StrictMock<ITache>();
    mouvement.StockPalOrigine = mocks.StrictMock<IStockPalette>();
    mouvement.ID = Guid.NewGuid();

    var rule = new EmplacementTransitoireRule(mouvement);
    manager.AddRule(rule);

    Expect.Call(manager.ValidateAll()).Return(true);

    mocks.ReplayAll();

    var all = manager.ValidateAll();

    mocks.VerifyAll();

    Assert.IsTrue(all);
}

this Test always fails ..

A: 

Typically I would set up expectations on the mock objects instead of merely assigning their properties.

 Tache tache = mocks.StrictMock<Tache>();
 Expect.Call( mouvement.Tache ).Return( tache );

Also, you may want to use the AAA (Arrange-Act-Assert) syntax for RhinoMocks -- I believe that StrictMock has been deprecated.

 Mouvement mouvement = MockRepository.GenerateMock<Mouvement>();
 Tache tache = MockRepository.GenerateMock<Tache>();

 mouvement.Expect( m => m.Tache ).Return( tache );
 tache.Expect( t => t.Value ).Return( 100 );  // or whatever

 ... test code...

 tache.VerifyAllExpectations();
 mouvement.VerifyAllExpectations();
tvanfosson
Ok I just tried that, but now i'm facing with another problem. I needed to Except results from a protected method can i use the InternalsVisibleTo Assembly Attribute in order to get this working with rhino mocks ?
Yoann. B