How can I use Rhino Mocks to inspect what values were passed to a method

andy picture andy · Jul 12, 2010 · Viewed 13.4k times · Source

I'm new to mocking, and I'm having a hard time solving an issue with UnitTesting.

Say I have this code:

public class myClass{

    private IDoStuff _doer;

    public myClass(IDoStuff doer){
        _doer = doer;
    }

    public void Go(SomeClass object){

        //do some crazy stuff to the object

        _doer.DoStuff(object) //this method is  void too
    }
}

Ok, so I want to UNIT test the Go method. I don't care what the _doer object does to the object once is gets it.

HOWEVER, I do want to inspect what the _doer object has received.

in PSEUDO code I want to achieve this:

[Test]
public void MyTest()
{
    IDoStuff doer = Mocker.Mock<IDoStuff>();
    Guid id = Guid.NewGuid();

    //test Go method
    new MyClass(doer).Go(new SomeClass(){id = id});

    Assert.AreEqual(id,MockingFramework.Method(DoStuff).GetReceived<SomeClass>().id);
}

Is this possible using Rhino, and if so, how do I achieve it?

cheers

Answer

Wim Coenen picture Wim Coenen · Jul 12, 2010

With the new Arrange/Act/Assert syntax:

[Test]
public void MyTest()
{
    // arrange
    IDoStuff doer = MockRepository.GenerateStub<IDoStuff>();
    MyClass myClass = new Myclass(doer);
    Guid id = Guid.NewGuid();

    // act
    myClass.Go(new SomeClass(){id = id});

    // assert
    doer.AssertWasCalled(x => x.DoStuff(
        Arg<Someclass>.Matches(y => y.id == id)));
}