Search code examples
c#unit-testingmockingrhino-mocks

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


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


Solution

  • 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)));
    }